HTTP POST from Spark

Ok I’ve tried everything. I’ve dug through the community looking for people whom have had the same issue, and there are a few close matches, but their solutions never seem to work for me…

I’ve tried the HttpClient library, and can’t get it to work. It keeps telling me connection failed.

I tried the web sockets library. Connection failed.

For the life of me I can’t seem to figure out what I’m doing wrong.

   #include "HttpClient/HttpClient.h"

// This #include statement was automatically added by the Spark IDE.
HttpClient client;
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;

IPAddress server(192,168,1,132);

void setup() {
    request.ip = server;
     request.port = 8084;
     client.get(request, response, headers);

void loop() {

I’m using a local server on my home network. I tried using my “real” IP and port forwarding through my router… Nothing. I tried giving it an IP as was suggested in another thread… Nothing.

Anyone have any ideas?

I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy

I’m not too sure, why you do your call to client.get() only once in setup().

The Example usage code would suggest to do it repeatedly every 10sec inside loop().
Just give this code a try and then work your way down to your own use case.

Can you provide some more info about your use case. What response are you expecting from what server and what will you do with the response?
In your code, I can just see some attempted request, but no response handling.

One possible issue might be that you don’t set request.hostname and request.path but seem to assume, that it will be NULL or empty by default. But C/C++ does not take care of that. For this reason a comment in the implementation of HttpClient states this:

    // NOTE: The default port tertiary statement is unpredictable if the request structure
    // is not initialised
    // http_request_t request = {0} or memset(&request, 0, sizeof(http_request_t)) should 
    // be used to ensure all fields are zero

I’d not use the {0} initialisation but either do it via memset or fully qualified (each seperate field).

1 Like