ArduinoHttpClient response code of "-2" when preforming a get request

I am using an esp32 and the ArduinoHttpClient to communicate with JPL's Horizons API in order to get some planetary ephemeris. The problem is whenever I attempt to preform a get request I get a status code of -2 (I know that a -2 response is a "HTTPC_ERROR_SEND_HEADER_FAILED" error but I am not sure what that refers too) and nothing in the response body. The HttpClient works with the other API requests I perform, it's just when I try to do one with the horizons API.

WiFiClient wifi;


char ipifyAddress[] = "api.ipify.org";  // server address
char ipStackAddress[] = "api.ipstack.com";  // server address
char horizonsAddress[] = "ssd.jpl.nasa.gov"; 
int port = 80;

HttpClient client = HttpClient(wifi, ipifyAddress, port);
HttpClient locationClient = HttpClient(wifi, ipStackAddress, port);

HttpClient horizonsClient = HttpClient(wifi, horizonsAddress, port);


void horizons(double* longLat, String* day){

 
  String cTCheck = "/api/horizons.api?format=text&COMMAND='499'";

  horizonsClient.get(cTCheck);
  
  
  int statusCode = horizonsClient.responseStatusCode();
  String response = horizonsClient.responseBody();


  Serial.println(horizonsClient.contentLength());


  Serial.print("Status code: ");
  Serial.println(statusCode);


  Serial.print("Response: ");
  Serial.println(response);
 
  horizonsClient.stop();
  
}

I have attempted to modify the address I perform the get request from including just "/api/horizons.api", which should return a "lack of commands" return from the API but even that still returns a -2.

I also used the .stop() function with the other httpClients once their tasks are complete so I doubt the other functions are causing a problem. I also tried escaping the single quotes but that doesnt make a different either.

Thanks.

In my version of the library -2 translates to something else:

// This call was made when the HttpClient class wasn't expecting it
// to be called.  Usually indicates your code is using the class
// incorrectly
static const int HTTP_ERROR_API =-2;

Post your complete code, experience shows that the error is usually in that part of the code that people are hiding from us.

One problem might be that the library expects exclusive control over the wifiClient object but you're sharing it between the two instances of HttpClient.

Having a look at NASA Horizons APIs docs I see the API should be called in https, not http:
image

and port 443 if https ?

Yeah, 443 is the default TCP port for https, but you can't just change the port number, the protocol is not the same, it has to do with certificates and encryption, so you need to try using WiFiClientSecure Library (I know exists even if I have never used it).

Read more about https and ESP32 HERE for example.

1 Like

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