HttpClient.get() returns response.status -1

Hello there!
I'm trying to send a GET request to the web page using HttpClient library. I created my code according to examples in the docs and a repo of the library. And that just doesn't work, response.status is always -1 (once it was 404 when I requested a page that doesn't exist)... Maybe someone knows why it works in this way?

#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;

String result; 

void setup() {
    Particle.variable("result", result);
    request.hostname = "";
    request.path = "/";

    http.get(request, response, headers);
    result = response.status;

void loop() {

The device I'm using is Boron LTE.

The negative status values indicate that the error was not sent from the target server but was created from the HttpClient library.
You'd need to look in the library code to see in what cases a -1 status gets produced.

Unfortunately the library uses -1 as a catch-all instead of returning distinct values depending on where the call failed.

But one thing is certain: http:// is not part of the host name but the protocol denominator - which is superfluous here for the fact that this library only supports HTTP anyhow :wink:

1 Like

Are there any other libs I can use to send an HTTP request?

You can "import" the library sources and activate logging to see where the error comes from.

But have you tried removing http:// from your host name?

Unfortunately, I haven't access to the device physically and can debug the code only using Particle.variable() and CLI... how I can log the error in that case?

Yep, tried a few minutes ago. The result is the same.

You can alter the library and add a line like

  aResponse.status = <someDistinctValue>;

before any of the return statements inside HttpClient::request().
This way you will find out where the function bailed and learn from that how to possibly mitigate the issue.

1 Like

I understood and will try it now! :saluting_face:

Could I ask you to help me with it? Because I downloaded sources, and created a project with my code locally (working in VSC with installed Workbench extension), but can't compile the project :frowning:

You should not state the full path for the header file. Just write

#include "HttpClient.h"

The Particle Workbench knows to look in the lib folder already.

To make sure that your altered library is used even when building in the cloud, you should comment/remove the respective dependency entry in the file.

I also think to recall that there were issues with OneDrive and also with paths containing blanks (i.e. your user name).

1 Like

I don't know how (what is bad), but yesterday it started working without any changing in the library. Maybe a cellular connection was bad at times when I got -1.
Thanks for the help, Mr ScruffR!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.