[SOLVED] HTTP Post Request for a MKR Wifi 1010 Board

Hello there !

I’m trying to make a HTTP post request for a connected device in order to switch it off or on.

Using Postman, no problem, it works verywell :

POST http://192.168.1.159/config

Headers:

Host:192.168.1.159
Content-Type:application/json
Connection:close
Accept-Encoding:gzip
User-Agent:isosel
Content-Type:application/json
Content-Length:308
Accept:/
Cache-Control:no-cache

The body: (also my string postData)

{“header”:{“from”:"/app/339299-e1853779db9fc7866cf6dfe0b697b0b8/subscribe",“messageId”:“36f3d4b5da1545dd127176bdbbf8f2cf”,“method”:“SET”,“namespace”:“Appliance.Control.ToggleX”,“payloadVersion”:1,“sign”:“195a5ea4fb950c3cb475cd5daa5d4e64”,“timestamp”:1571919319},“payload”:{“togglex”:{“channel”:0,“onoff”:1}}}

When I modify tge “onoff”:1 from 1 to 0, for exemple, it switchs my device OFF but now Im trying to do the same using a MKR Wifi 1010 Board… I tried a lot of different code, none works… it’s frustrating because idk what is wrong within my code…

WiFiClient myWifi;
WebSocketClient myClient = WebSocketClient(myWifi, server, 80);

if (myClient.connect(server, 80)) // server is a string: “192.168.1.159”
{
Serial.println(“Connected!”);
myClient.println(“POST /config HTTP/1.1”);
myClient.println(“Connection: close”);
myClient.println(“Content-Type: application/json”);
myClient.println(“Content-Length: 308”);
myClient.println(“Host: 192.168.1.159”);
myClient.println(“Accept-Encoding: gzip”);
myClient.println(“User-Agent: okhttp/3.6.0”);
myClient.println();
myClient.print(postData); //the json string
Serial.println(“Request sent.”);
codeResponse = http1.responseStatusCode(); // it returns me -2, HTTP_ERROR_AP, what is it ?
}

Any idea plz ?

Im suffering

i also tried this:

#include <ArduinoHttpClient.h>

WiFiClient myWifi;
HttpClient http1 = HttpClient(myWifi, serverMSS210, portMSS210);

http1.beginRequest();
http1.post("/config");
http1.sendHeader(“Connection”, “close”);
http1.sendHeader(“Content-Type”, “application/json”);
http1.sendHeader(“Content-Length”, “308”);
http1.sendHeader(“Host”, “192.168.1.159”);
http1.sendHeader(“Accept-Encoding”, “gzip”);
http1.sendHeader(“User-Agent”, “okhttp/3.6.0”);
http1.beginBody();
http1.print(postData);
http1.endRequest();

Thx in advance :slight_smile:

I keep you in touch if i fond something

using http1 after you sent the request raw over client doesn't make sense. read the response on myClient.

is the server on same computer as the postman client. can you 'postman' from another computer? is the port on firewall open?

with httpclient let the httpclient handle the Content-length header. remove the accept encoding header. you don't want gziped answer

Hello !

I used Postman on my personal computer which is connected to my network.

The connected device is an electrical plug that can be switched on or off in order to control a light bulb. This plug is also connected to my network.

The Arduino Board, the MKR Wifi 1010 is connected to my network too using the WiFiNINA.h library.

I used the ArduinoHttpClient.h library in order to create the http client object named "http1". Regarding the library documentation and some videos, the process to send a http request is as before, create an object "WiFi" then an object http...

I just followed an example available here:https://github.com/arduino-libraries/ArduinoHttpClient/blob/master/examples/PostWithHeaders/PostWithHeaders.ino

I need gziped encoding using postman so why not using HttpClient ?

Regards,

Isosel.

isosel: Hello !

I used Postman on my personal computer which is connected to my network.

The connected device is an electrical plug that can be switched on or off in order to control a light bulb. This plug is also connected to my network.

The Arduino Board, the MKR Wifi 1010 is connected to my network too using the WiFiNINA.h library.

I used the ArduinoHttpClient.h library in order to create the http client object named "http1". Regarding the library documentation and some videos, the process to send a http request is as before, create an object "WiFi" then an object http...

I just followed an example available here:https://github.com/arduino-libraries/ArduinoHttpClient/blob/master/examples/PostWithHeaders/PostWithHeaders.ino

I need gziped encoding using postman so why not using HttpClient ?

Regards,

Isosel.

because you don't have code to unzip the answer.

print the response to see what error is sent.

impossible to see the answer, it returns me -3 only even if I remove the encoding header...

My POST request works perfectly using POSTMAN... idk what is wrong :/

I use ArduinoHttpClient.h, the code is below:

IPAddress server(192,168,1,192); int port = 80; WiFiClient wifi; HttpClient client = HttpClient(wifi, server, port);

Serial.println("making POST request"); String postData = "{\"header\":{\"from\":\"/app/339299-e1853779db9fc7866cf6dfe0b697b0b8/subscribe\",\"messageId\":\"36f3d4b5da1545dd127176bdbbf8f2cf\",\"method\":\"SET\",\"namespace\":\"Appliance.Control.ToggleX\",\"payloadVersion\":1,\"sign\":\"195a5ea4fb950c3cb475cd5daa5d4e64\",\"timestamp\":1571919319},\"payload\":{\"togglex\":{\"channel\":0,\"onoff\":0}}}";

client.beginRequest(); client.post("/config"); client.sendHeader("Content-Type", "application/json; charset=UTF-8"); client.sendHeader("Content-Length", postData.length()); client.sendHeader("Connection", "close"); client.sendHeader("Accept-Encoding", "gzip"); client.sendHeader("User-Agent", "arduino"); client.sendHeader("Cache-Control", "no-cache"); client.sendHeader("Accept", "/"); client.beginBody(); client.print(postData); client.endRequest();

int statusCode = client.responseStatusCode(); String response = client.responseBody();

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

It doesnt work at all :/

is "client.sendHeader("Content-Type", "application/json; charset=UTF-8");" correct ?

Regards,

isoSeL.

and what statusCode does it return now?

static const int HTTP_SUCCESS =0;

// The end of the headers has been reached.  This consumes the '\n'
// Could not connect to the server
static const int HTTP_ERROR_CONNECTION_FAILED =-1;

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

// Spent too long waiting for a reply
static const int HTTP_ERROR_TIMED_OUT =-3;

// The response from the server is invalid, is it definitely an HTTP
// server?
static const int HTTP_ERROR_INVALID_RESPONSE =-4;

Hello !

ArduinoHttpRequest.h library doesn’t work verywell for post functions.

When I want to use the post function client.post("/test"), it sends over the wifi traffic 2 TCP segments which are reassembled, based on Wireshark analysis.

The 1st one contains “POST” (4 bytes), the 2nd contains the rest of the request… This is mainly why my request doesn’t work, using ArduinoHttpRequest.h

I reworked my program and now it works perfectly. I used only WiFiNINA.h to perform a post request over my wifi network.

the code is below:

WiFiClient myClient;

if (myClient.connect(serverMSS210, portMSS210))
{
Serial.println(“Connected !”);

myClient.print // the space between Content-Type: and application/json is needed to make it works with my device
(
String("POST “) + “/config” + " HTTP/1.1\r\n” +
//"Host: " + “192.168.1.180” + “\r\n” + // it works with an other Host ip… and without this line
“Content-Type: application/json\r\n” +
"Content-Length: " + postData.length() + “\r\n” + //this line is needed with the exact size value
//“Accept-Encoding: gzip\r\n” + // it works without this line
//“User-Agent: ISOSEL\r\n” + //"User-Agent: Arduino/2.2.0\r\n but it works with an other user-agent and without this line
//“Accept: /\r\n” + // it works without this line
//“Cache-Control: no-cache\r\n” + //it works without this line
//“Connection: close\r\n” + // it works without this line
“\r\n” + // CR+LF needed to notify that is the end of the header
postData
);

while(myClient.available()==0)//wait until data reception
{
; // you can add a counter and break if you exceed a value
}
while(myClient.available()) //write all incoming data characters on serial monitor
{
codeResponse = myClient.read();
Serial.write(codeResponse);
}
}

answer provided by the device:

HTTP/1.1 200 OK
Content-Type: application/json

{“header”:{“messageId”:“36f3d4b5da1545dd127176bdbbf8f2cf”,“namespace”:“Appliance.Control.ToggleX”,“method”:“SETACK”,“payloadVersion”:1,“from”:"/appliance/1907186828259425184948e1e901429c/publish",“timestamp”:1573035566,“timestampMs”:776,“sign”:“ac6047bc98575678bc4c6b67717b8b84”},“payload”:{}}

my github:

Best regards,

isoSeL.