Http post in the particle IDE

Hey people. As you may know, I asked about the differences between spark.publish and http post the other day. I decided to use http post and I was looking around for a detailed explanation as to how to use it but I could not find anything. Is there any web site out there that has a good tutorial or instructions with using http post? thanks in advance.

Heres an example on how to make a POST request.
Note that the http library may be outdated in the cloud, so you need to fork it and include your local one. (@Particle we really need abandoned libs be updated automatically or by admins)

#include "application.h"
#include "HttpClient/HttpClient.h"

/**

  • Declaring the variables.
    */
    unsigned int nextTime = 0; // Next time to contact the server
    HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers = {
// { "Content-Type", "application/json" },
// { "Accept" , "application/json" },
{ "Accept" , "/"},
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};

http_request_t request;
http_response_t response;

void setup() {
Spark.publish("log", "Application>\tStart of Loop.");
// Request path and body can be set at runtime or at setup.
request.hostname = "requestb.in";
request.port = 80;
request.path = "/169h3qh1";

// The library also supports sending a body with your request:
request.body = "?key=value";
// Get request
http.post(request, response, headers);
Spark.publish("log", "Application>\tResponse status: ");
//Spark.publish("log", response.status);
Spark.publish("log", "Application>\tHTTP Response Body: ");
Spark.publish("log", response.body);

}

void loop() {

}

Ok, thanks. Could you please explain the part of the code with the path?

request.path = "/169h3qh1";

Like, where did the path come from. What is it for?

Hi @SlinkyMation

If you have a URL like this:

http://my.host.com/first/second/third

Then the path you want to put in request.path is:

request.path = "/first/second/third";

[edit: I took the .html out of this for clarity]

1 Like

The 169h3qh1 was just my id from http://requestb.in a very handy service for debugging http requests.

As bko says its the path of the url you are calling.

Ok, question: I have copied the exact code from this link:
http://ubidots.com/docs/devices/spark.html

But I changed it so that I could upload it to my companies website. To keep the company anonymous. Let’s say the web site is called mywebsite.com. Here is the code that I used:

#include "HttpClient.h"
#include "dnsclient.h"          // Add this library if your Spark is having problems with DNS

#define TOKEN "17858206d57ab4f334ea296f70621f36a9daa212"

HttpClient http;
float lightLevel = 0;
char resultstr[64];

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    { "Host", "mywebsite.com" },     // Declare the host here if your Spark has DNS problems
    { "Content-Type", "application/json" },
    { "X-Auth-Token" , TOKEN },
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};

http_request_t request;
http_response_t response;

//Uncomment these lines if your Spark is having problems with DNS resolution:
// ------------------------DNS-FIX--------------------------------------------
IPAddress dnsServerIP(8,8,8,8);
IPAddress remote_addr;
DNSClient dns;
char serverName[] = "mywebsite.com";
// ---------------------------------------------------------------------------

void setup() {
    pinMode(A0, INPUT);
    request.port = 80;
//    dns.begin(dnsServerIP);                      // Uncomment if having DNS problems
//    dns.getHostByName(serverName, remote_addr);  // Uncomment if having DNS problems
//    request.ip = remote_addr;                    // Uncomment if having DNS problems
    request.path = "/viewDevices";
    Serial.begin(9600);
}

void loop() {

    // Get variable

    lightLevel = analogRead(A0);

    // Send to Ubidots

    sprintf(resultstr, "{\"value\":%.4f}", lightLevel);
    request.body = resultstr;
    http.post(request, response, headers);

    Serial.println(response.status);
    Serial.println(response.body);

    delay(1000);
}

When I run this it does not upload it to my website. I know the access token is correct. Also, when I look at the serial terminal, all I am getting is “-1” about every 7 seconds. Does anyone know why this is not working?

Ok, so I changed it and took out the // in front of

//    dns.begin(dnsServerIP);                      // Uncomment if having DNS problems
//    dns.getHostByName(serverName, remote_addr);  // Uncomment if having DNS problems
//    request.ip = remote_addr;                    // Uncomment if having DNS problems

And now in the serial terminal, I am getting this message:

Not Allowed</h1>
</li>
                                                         
                 <ul>
 <li>RequestId: 28FAFA7E60946CC5</li>
                    
                      <li>Code: MethodNotAllowed</li>
      <li>HostId: /nrVAmAE
5k0V6EiM3VK+4yYyyEPZfjxqlfWkiKJV07Hp8he5i7usZ0wCpc5prM<li>Message: The specified
 method is not allowed against this resource.</li>
                       </ul>

                                                   <li>Method: POST</li>
      <
hr/>
                                                                    <li>Res
ourceType: OBJECT</li>
                                                         
             </html>
  <li>RequestId: 61C58B1D2342F208</li>
                    
                     
                                     <li>HostId: UUwWhKLs
iRa+25gsEZk4uTiKv15LLZEZTsRzUmfNxI8BQpzsD5QMeNb/qIo4BCTV</li>
                  
                                                              </ul>
            
                                                                    <hr/>
      
                                                                          </body

So yeah, I still don’t know the problem haha

You got an error back from the host you are trying to POST to saying that what you did was not allowed.

Maybe you should make sure you can do your POST request to this host with a desktop computer first using curl or similar program.

Then you can make your Particle code match, possibly by creating a temporary testing area to post to on requestb.in so you can see exactly what your device is sending.

1 Like

Are you sure that you need POST and not GET or even PUT ?