[Resolved] Particle Electron + HTTP POST

Hi,

Has anyone had any luck with a HTTP Post Library and a Particle Electron? I tried the ‘HttpClient’ library with no luck. A simple code snippet sending to pastebin via HTTP Post would be fab, if anyone has managed it.

…and yes I’m aware this can be done without HTTP Post with Webhooks :wink:

@joearkay, I had no problems using the HTTPClient library on an Electron. I can’t share my code (NDA) but what problems are you having?

Hi @peekay123, thanks for the response. I’ve copied the original example, changed to HTTP Post, attempting to Post to a requestbin, with no luck. Serial debugging is on.

Here’s my code:

    // This #include statement was automatically added by the Particle IDE.
#include "HttpClient.h"

#include "application.h"


/**
* Declaring the variables.
*/

HttpClient http;

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

http_request_t request;
http_response_t response;

void setup() {
    Serial.begin(9600);
}

void loop() {
  

    Serial.println();
    Serial.println("Application>\tStart of Loop.");
    // Request path and body can be set at runtime or at setup.
    request.hostname = "http://requestb.in";
    request.port = 80;
    request.path = "/abcdefg";

    // The library also supports sending a body with your request:
    request.body = "{\"key\":\"value\"}";

    // Get request
    http.post(request, response, headers);
    Serial.print("Application>\tResponse status: ");
    Serial.println(response.status);

    Serial.print("Application>\tHTTP Response Body: ");
    Serial.println(response.body);
    delay(10000);


}

And here’s the printing from the serial monitor:

Application>    Start of Loop.
Application>    Response status: -1
Application>    HTTP Response Body:

I can’t for the life of me work out what is wrong! Just to clarify, this is a ‘Particle Electron’. @nmattisson could you provide some feedback is it’s your library? :smiley:

Cheers

I’ve solved it!

It was completely trivial, but for anyone with a similar problem…it was the ‘http’ in my hostname, it should be:

request.hostname = "requestb.in";
request.port = 80;
request.path = "/abcdefg";

Thanks all for the help!

1 Like

@peekay123 Turns out the REST Server I’m contacting only accepts HTTPS connections…after all of this! And as far as I’m aware, there isn’t an easy way of implementing HTTPS on a Particle…

Hi @joearkay

Have you seen this post by the Glowfish team?

There is also work in MBED on HTTPS.

1 Like

Is this usable on the Electron?

Hi @joearkay

I believe that the code should work, but I have not tested it. Get ready for high data usage since that is what happens with HTTPS.

1 Like

It should be. Alternatively there are also webhooks that enable HTTPS.

2 Likes

That’s what I’m worried about!