Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By jimthree
#18667 Hi Folks.

Can anyone help me, I'm trying to construct a POST request to post data to a web service. Does anyone know of any examples they can point me to?

I have a RESTful endpoint that I want to POST to at api.example.com/test (it's using the ArrestDB library). The Body should contain two fields, "device_id" and "value". I understand the basics of URLEncoded or JSON encode body data, but I can get my head round how to construct request (URL+Body).

thanks
Jim
User avatar
By jimthree
#18705 Yes, that looks like a great pointer, thank you. I'll post up my results (if) when I get it working.

cheers
Jim
User avatar
By tytower
#18709 viewtopic.php?f=6&t=2872&p=18357#p18357

In there there is a postData function to Sparkfuns server

Code: Select allvoid postData()
{
 
  // Make a TCP connection to remote host
  if (client.connect(server, 80))
             {
    // Post the data! Request should look a little something like:
    // GET /input/publicKey?private_key=privateKey&light=1024&switch=0&name=Jim HTTP/1.1\n
    // Host: data.sparkfun.com\n
    // Connection: close\n
    // \n
    client.print("GET /input/");
    client.print(publicKey);
    client.print("?private_key=");
    client.print(privateKey);
    for (int i=0; i<NUM_FIELDS; i++)
              {
      client.print("&");
      client.print(fieldNames[i]);
      client.print("=");
      client.print(fieldData[i]);
               }
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
  //Serial.println( fieldNames[],fieldData[]); 
   
            }
  else
        {
    #ifdef debug
    Serial.println(F("Connection failed"));
    #endif
         }
  while (client.connected())
  {
    if ( client.available() )
    {
      char c = client.read();
      #ifdef debug
      Serial.print(c);
      #endif
    }     
  }
 #ifdef debug
  Serial.println();
  #endif
  client.stop();
}