arduino-libraries / ArduinoHttpClient

Arduino HTTP Client library
288 stars 172 forks source link

post request with application/json; #76

Closed isosel closed 5 years ago

isosel commented 5 years ago

Hello im trying to make a POST request to a device with a MKR Wifi 1010 Board.

POST request to http://192.168.192:80/config

headers:

Host:192.168.1.192 Content-Type:application/json; charset=UTF-8 Connection:close Content-Length:308 Accept-Encoding:gzip User-Agent:arduino Accept:/ Cache-Control:no-cache

body:

{"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}}}

Using postman, the device react correctly, as expected...

I encounter some difficulties using arduino with this library...

code below:

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

Serial.begin(115200); // I use this baud rate, significant for http request ??

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();

This code doesn't work ... but Postman is doing well... idk why and it's boring ._.

I ask you your help for this issue, im suffering...

Regards,

isoSeL

Cy3rian0 commented 5 years ago

I don't have a solution for your problem my friend, just wanted to tell you not to suffer! You're not alone. I have a similar problem with my Arduino where Postman works fine, yet my script doesn't. I'm guessing though that I think I'm sending Content - Type when actually I'm not. Remember, software doesn't do what we want, it does what we tell it to do. In my case, as my friends say, the problem must be between the chair and the keyboard. :-D

isosel commented 5 years ago

solved

isosel commented 5 years ago

https://github.com/isosel/meross-mkr1010/blob/master/code.ino