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 ![]()
I keep you in touch if i fond something