working HTTP POST example

I wanted to write some code that would POST a bunch of analog variables to my webserver. I couldn't find a great POST example, so I muddled through it for a couple hours. My code is working, so I'm posting it as a reference to anyone in the same spot.

This example builds and sends a POST request to my server every .5 seconds. It doesn't bother to look at the response, just barfs data into the server like there's no tomorrow. I get the sense that not all my requests make it to the server, because the timestamps of my data in the server aren't evenly spaced. Still! It's data, going up!

I'm using a wifly shield, but this should work for anyone using the httpclient or Ethernet libraries.

Something I didn't know before writing this--POST parameters are serialized just like GET parameters, and they're just listed in the "content" part of the HTTP request. For example, if you want to POST the parameters "larry"=5, "curly"=6, "moe"=7, your POST content just looks like

larry=5&curly=6&moe=7

how cool is that?

Hope this helps somebody out. Happy Hacking!
--enjrolas

#include "WiFly.h"
#include "Credentials.h"

Timer t;
long lastUpdate=0;

byte server[] = { 106,187,94,198 }; // artiswrong.com

Client client("artiswrong.com", 80);

void setup() {
  
  Serial.begin(115200);

  WiFly.begin();
  
  if (!WiFly.join(ssid, passphrase)) {
    Serial.println("Association failed.");
    while (1) {
      // Hang on failure.
    }
  }  

}

void post()
{
  Serial.println("connecting...");
  String PostData="sample={\"fittingId\":1,";
  unsigned char i;
  for(i=0;i<6;i++)
  {
    PostData=PostData+"\"channel-";
    PostData=String(PostData+i);
    PostData=PostData+"\":";
    PostData=String(PostData + String(analogRead(i)));
    if(i!=5)
      PostData=PostData+",";
  }
    PostData=PostData+"}";  
  Serial.println(PostData);
  if (client.connect()) {
    Serial.println("connected");
  client.println("POST /tinyFittings/index.php HTTP/1.1");
  client.println("Host:  artiswrong.com");
  client.println("User-Agent: Arduino/1.0");
  client.println("Connection: close");
  client.println("Content-Type: application/x-www-form-urlencoded;");
  client.print("Content-Length: ");
  client.println(PostData.length());
  client.println();
  client.println(PostData);
  } else {
    Serial.println("connection failed");
  }
}


void loop() {
    post();
    delay(500);
}

Thanks for sharing!

Very interesting post !

Could you also tell us more details about your configuration :

  • Arduino board model
  • Arduino IDE used and the reference (link to download) of the librairie Wifly
  • version number of your Wifly firmware

Thank you in advance.

Thanks for taking the time to post this source. It really helped me get started. On my server (ASP.NET MVC 4), I needed to replace this line:

client.println("Content-Type: application/x-www-form-urlencoded;");

with the following:

client.println("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");

in order to get it to work without an error 500. Posting this just in case others run into the same issue.

1 Like

the code in one of the projects i made supports both POST and GET commands as the arduino runs a full web-server within itself. the code is rather complicated as it carefully breaks the HTTP headers apart thanks to a tutorial i found here: Arduino Playground - WebServer where you need to scroll to "Extended Functionality"
and the code works very very well.

http://forum.arduino.cc/index.php?topic=140740.0

I'm sending data to a webserver, and the webserver stores in mysql database. How i return back the stuff (ok) variable to arduino and make a Serial Print(#stuff)?

String request = "GET "+ repository + "sensor.php?value=" + value + " HTTP/1.0";
  send_request(request);

  void send_request (String request) {
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, port);
  if (client.connected()) {
    client.println(request);      
    client.println(F(""));
    Serial.println("Connected & Data sent");
  } 
  else {
    Serial.println(F("Connection failed"));
  }
 string buffer;
int counter = 0;
while (client.connected()) {
  while (client.available()) {
   buffer[counter++] = client.read();
  }
}
Serial.println("Closing connection");
Serial.println("Buffer value is: " + buffer);
client.close();
}

PHP FILE:

$stuff = "";
include("conec.php");
if ($_GET["value"]) {
$link=Conection();
$Sql="insert into table (VALUE) values ('".($_GET["value"])."')"; 
mysql_query($Sql,$link);
$stuff = "ok";
}

Could someone modify the Sketch to Send a Post to something Public like http://requestb.in/ using the Official Ethernet.h library?

This is the First Page on Google when looking for "HTTP POST Arduino Example" and maybe a more general sketch could help me and all the other users trying to figure this out?

Second example here.
http://playground.arduino.cc/Code/WebClient

if I put a post on developers forum what will be the response time?

enjrolas:
I wanted to write some code that would POST a bunch of analog variables to my webserver. I couldn't find a great POST example, so I muddled through it for a couple hours. My code is working, so I'm posting it as a reference to anyone in the same spot.

This example builds and sends a POST request to my server every .5 seconds. It doesn't bother to look at the response, just barfs data into the server like there's no tomorrow. I get the sense that not all my requests make it to the server, because the timestamps of my data in the server aren't evenly spaced. Still! It's data, going up!

I'm using a wifly shield, but this should work for anyone using the httpclient or Ethernet libraries.

Something I didn't know before writing this--POST parameters are serialized just like GET parameters, and they're just listed in the "content" part of the HTTP request. For example, if you want to POST the parameters "larry"=5, "curly"=6, "moe"=7, your POST content just looks like

larry=5&curly=6&moe=7

how cool is that?

Hope this helps somebody out. Happy Hacking!
--enjrolas

Very late (as I only came across this post) due to looking for methods of using arduino to POST values (data) to a web form.

One comment - there's a mention of "larry=5&curly=6&moe=7"

However, I can't see any '&' symbol in your code. So what does "larry=5&curly=6&moe=7" mean?

Thanks.

#include "WiFly.h"
#include "Credentials.h"

Timer t;
long lastUpdate=0;

byte server[] = { 106,187,94,198 }; // artiswrong.com

Client client("artiswrong.com", 80);

void setup() {
 
  Serial.begin(115200);

  WiFly.begin();
 
  if (!WiFly.join(ssid, passphrase)) {
    Serial.println("Association failed.");
    while (1) {
      // Hang on failure.
    }
  } 

}

void post()
{
  Serial.println("connecting...");
  String PostData="sample={\"fittingId\":1,";
  unsigned char i;
  for(i=0;i<6;i++)
  {
    PostData=PostData+"\"channel-";
    PostData=String(PostData+i);
    PostData=PostData+"\":";
    PostData=String(PostData + String(analogRead(i)));
    if(i!=5)
      PostData=PostData+",";
  }
    PostData=PostData+"}"; 
  Serial.println(PostData);
  if (client.connect()) {
    Serial.println("connected");
  client.println("POST /tinyFittings/index.php HTTP/1.1");
  client.println("Host:  artiswrong.com");
  client.println("User-Agent: Arduino/1.0");
  client.println("Connection: close");
  client.println("Content-Type: application/x-www-form-urlencoded;");
  client.print("Content-Length: ");
  client.println(PostData.length());
  client.println();
  client.println(PostData);
  } else {
    Serial.println("connection failed");
  }
}


void loop() {
    post();
    delay(500);
}

Thanks for this quick and dirty code

Hello, community

not everyone can take the time to set up a public server and learn all the programming required. I set up a free server for creators to use. This server gives you a free place to test and learn HTTP requests and a foundation into the Internet of things communications.

I would appreciate some feedback. https://enochs.world/iotServer/iotMain