I was developing some code for my Arduino Nano IoT 33, I want it to connect to a server I'm running. To do some testing I'm using ngrok to expose my localhost 80 port.
The Arduino should be able to do a POST, a GET and a PUT request in this specific order. To do so I'm using the ArduinoHttpClient library.
I had no issues making the POST request, using this code:
client.beginRequest();
client.post(tokenUri, contentType, body);
String auth = client.responseBody();
Serial.println("authentication:");
Serial.println(auth);
It returns the correct payload that is supposed to return.
But, for this get request:
client.beginRequest();
client.get("/api/rest/device/ArduinoFirstTest/command/poll?waitTimeout=3");
client.sendHeader(header);
delay(500);
String Command = client.responseBody();
delay(200);
int statusCode = client.responseStatusCode();
Serial.println("Get command poll response");
Serial.println(Command);
Serial.println("Get command status code:");
Serial.println(statusCode);
delay(200);
client.endRequest();
the server returns the following:
[13/Oct/2023:09:22:45 +0000] "GET /api/rest/device/ArduinoFirstTest/command/poll?waitTimeout=3 HTTP/1.1" 400 0 "-" "Arduino/2.2.0" "-"
So I tried with another GET request, that does not require any authorization header to be sent, to see if that would work.
client.beginRequest();
client.get(pingUri);
delay(200);
String ping = client.responseBody();
Serial.println("Ping");
delay(200);
Serial.println(ping);
delay(200);
client.endRequest();
The error this time is 499.
I tried to specify the uri while sending the header to see if that was the problem, but it didn't work.
Also, since I'm not really an arduino expert i tried to do the same calls with python
response= requests.get(ngrokLink,headers=headers)
print(response.json())
This gives me no issues at all and works perfectly.
solved, like Juraj said endRequest should be before reading the response.