Null response when POSTing to server with HTTPclient

I’m trying to POST a few values using httpclient. This is the code I’m using:

#include "application.h"
#include "HttpClient/HttpClient.h"
http_header_t headers[] = {
      { "Content-Type", "text/html" },
      { "Accept" , "*/*" },
      { "User-agent" , "particle" },
    { NULL, NULL }
};
HttpClient http;
http_request_t request;
http_response_t response;

void setup() {
    request.hostname = "74.125.69.121"; //Also tried time.org, and putting in the domain for my server
    request.port = 80;
    request.path = "/u";
}


void loop() {
    request.body = "{\"pass\":\"redacted\"}";
    http.post(request, response, headers);
    Spark.publish(String(response.body));
    delay(10000);
}

Unfortunately, I don’t get a request hit at my server, and the response is always ‘null’. I have tried using the IP address, using a different server (time.org), or using the domain name.

Any help would be much appreciated. Sorry if this is the wrong category for this.

EDIT: By the way; I can see on a spectrum analyzer that some wifi packets are being sent.

Sorry for being lazy and not checking if this is still true, but at one point it was :blush:

If you want to pass the IP address, you should not put it into the http_request_t::hostname field of the request but into the dedicated http_request_t::ip field.


Checked in the implementation of spark_wiring_tcpclient on which HTTPClient builds on top

int TCPClient::connect(const char* host, uint16_t port, network_interface_t nif) 
{
      int rv = 0;
      if(Network.ready())
      {
        IPAddress ip_addr;

        if(inet_gethostbyname(host, strlen(host), ip_addr, nif, NULL) == 0)
        {                
                return connect(ip_addr, port, nif);
        }
        else
            DEBUG("unable to get IP for hostname");
      }
      return rv;
}

So if you pass in an IP address as hostname, the inet_gethostbyname() will fail.

1 Like

Also for testing you might want to check the serial output and see if it’s connecting or getting an error… might help work out why

2 Likes

I’ll be sure to try this for testing. Thanks a lot. Unfortunately, I do need the hostname feature to work in the future, as I don’t have control over the IP of my server.

Do you happen to know if I can change which serial port (I need to get the serial info from the hardware Serial1 pins) the debug info gets sent to? I don’t have a USB port on my device, unfortunately.

Are you using the web IDE? Easiest way would be to load the library manually by using the little plus in the top right to add the tabs and cut and paste the library code in. Then at each place it says serial.print or serial.write change it to serial1.print… or write.

Are you using a P1? Do you have a photon you could test the httpclient code with just to get it working properly and then put it on the device without USB?

I just found the problem accidentally; if you’re using a domain with a prefix, like ‘www.something.somethingelse.com’, then omit the www. or else the library will be mad. Thanks everyone for all the help.

1 Like