arduino-libraries / ArduinoHttpClient

Arduino HTTP Client library
282 stars 170 forks source link

Problem with HTTP POST request #70

Closed tkralj closed 5 years ago

tkralj commented 5 years ago

Hello. I am having a problem when sending HTTP POST requests to my server. The code is as follows:

 SerialMon.println("making POST request");

String contentType = "application/x-www-form-urlencoded; charset=UTF-8";
String postData = "tid=acc7a95c-b91f-48ae-8a36-0fcd58ca2313&time=2019.05.25%2008:18:59&amount=2.00&rsnd=false&code=0000";

https.beginRequest();
https.post("/api/MyEndPoint?tid=acc7a95c-b91f-48ae-8a36-0fcd58ca2666&time=2019.05.09%2008:18:59&amount=2.00&rsnd=false&code=0000", contentType, postData);
https.sendBasicAuth("user","pass");
https.sendHeader("api-key", "3e7fc55a-451d-3e40-aff8-955aa3f84224");
https.endRequest();
SerialMon.println("END POST request");

The problem seems to be in line "https.sendHeader("api-key"). When I set this header the client times out (like in debug output below), but if i comment out this header my WebAPI endpoint returns "Not authorized" which is expected because it requires api-key to be set and valid. I am making a connection over SSL using TingyGSM module. GET requests works fine even with headers set and this same WebAPI works fine if I set it's attribute to be GET and not SET type on the IIS side. Can you help ?

Initializing modem... [7162] ### TinyGSM Version: 0.7.9 [7362] ### Unhandled: ⸮⸮ [7477] ### Modem: SIMCOM SIM800L [7477] ### Modem: SIMCOM SIM800L Modem: SIM800 R14.18 Waiting for network... OK Network connected Connecting to wm OK Performing HTTPS POST request... making POST request [22893] ### Available: 0 on 1 END POST request [26289] ### Available: 0 on 1 [27313] ### Available: 0 on 1 [28337] ### Available: 0 on 1 [29359] ### Available: 0 on 1 [30382] ### Available: 0 on 1 [31404] ### Available: 0 on 1 [32426] ### Available: 0 on 1 [33447] ### Available: 0 on 1 [34469] ### Available: 0 on 1 [35492] ### Available: 0 on 1 [36514] ### Available: 0 on 1 [37536] ### Available: 0 on 1 [38558] ### Available: 0 on 1 [39580] ### Available: 0 on 1 [40602] ### Available: 0 on 1 [41624] ### Available: 0 on 1 [42646] ### Available: 0 on 1 [43667] ### Available: 0 on 1 [44690] ### Available: 0 on 1 [45712] ### Available: 0 on 1 [46733] ### Available: 0 on 1 [47755] ### Available: 0 on 1 [48779] ### Available: 0 on 1 [49804] ### Available: 0 on 1 [50826] ### Available: 0 on 1 [51848] ### Available: 0 on 1 [52870] ### Available: 0 on 1 [53892] ### Available: 0 on 1 [54913] ### Available: 0 on 1 [55935] ### Available: 0 on 1 [56958] ### Available: 0 on 1 [57458] ### Available: 0 on 1 Response:

Server disconnected GPRS disconnected

tkralj commented 5 years ago

It turned out to be my mistake. If you send POST request you have to set Content-Length header (even there is no content) for this to work.

Azhar-2022 commented 11 months ago

@tkralj i am also getting similar problem can you provide your code or help me out ?