Sending HTTP body request to a specific URL
Posted: Wed Apr 22, 2020 6:28 am
Hi, Below is what i am trying to achieve,
I am trying to send a HTTP POST request to a specified URL where i have to post a request, for which i get respective response from the server. For example: If i post "{Sensor id: Sensor 1}"(in json format), i will receive a response data respective to sensor 1 and so on. If the sensor id is not sent along with the request, server will return an error message stating "Sensor id cannot be empty".
The request headers must contain Content-Type, Content-Length, Host,User-Agent,Accept-Encoding and Connection. The request body must contain "{Sensor id: Sensor 1}" in Json format.
Below is what i have done ,
I have used HTTPClient library - https://github.com/amcewen/HttpClient. So the content type must be "application/x-www-form-urlencoded". To send a body request i have used "POST" method and "addHeaders" method to add headers as specified in the Library.
[Codebox][/Codebox]
Below is the problem i am facing,
So, when i run the code , Esp is not sending the headers and body along with the request
This is what ESP32 is sending to the server " (-) - - [22/Apr/2020:06:03:24 +0000] "POST /api/getconfiguration/ HTTP/1.1" 200 70 "-" "ESP32HTTPClient" "
This POST request was captured and logged in server. As you may see, there are no headers and body (Sensor id in this case) which i actually intended to send.The sensor id and the headers is not added along with the request. Resulting in error response from the server stating that "Sensor Id cannot be empty". So, to test the server, i used an app in my PC to send request and worked well.
Below is what i need,
I need help in sending a body request to a specified URL using any library or API . Thanks in advance.
I am trying to send a HTTP POST request to a specified URL where i have to post a request, for which i get respective response from the server. For example: If i post "{Sensor id: Sensor 1}"(in json format), i will receive a response data respective to sensor 1 and so on. If the sensor id is not sent along with the request, server will return an error message stating "Sensor id cannot be empty".
The request headers must contain Content-Type, Content-Length, Host,User-Agent,Accept-Encoding and Connection. The request body must contain "{Sensor id: Sensor 1}" in Json format.
Below is what i have done ,
I have used HTTPClient library - https://github.com/amcewen/HttpClient. So the content type must be "application/x-www-form-urlencoded". To send a body request i have used "POST" method and "addHeaders" method to add headers as specified in the Library.
[Codebox]
Code: Select all
#include <WiFi.h>
#include <HTTPClient.h>
String jsonData = "Sensorid:\"Sensor-1\"";
const char* ssid = "SSID";
const char* password = "PASSWORD";
int httpCode;
HTTPClient http;
String Send;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println(WiFi.localIP());
Serial.println("Connected to the WiFi network");
}
void loop() {
http.begin("Some Url");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Content-Length","17");
http.addHeader("Host","Some Host");
http.addHeader("User-Agent","Espressif");
http.addHeader("Accept", "*/*");
http.addHeader("Accept-Encoding", "gzip, deflate, br");
http.addHeader("Connection","keep-alive");
httpCode = http.POST(jsonData); //jsonData = "Sensorid:\"Sensor-1\"";
if (httpCode > 0) { //Check for the returning code
String payload = http.getString(); //Get the response from the server
Serial.println(jsonData); // Print the request sent
Serial.println(payload); // Print the received response message from server
http.end(); //Free resource
}
delay(3000);
}
Below is the problem i am facing,
So, when i run the code , Esp is not sending the headers and body along with the request
This is what ESP32 is sending to the server " (-) - - [22/Apr/2020:06:03:24 +0000] "POST /api/getconfiguration/ HTTP/1.1" 200 70 "-" "ESP32HTTPClient" "
This POST request was captured and logged in server. As you may see, there are no headers and body (Sensor id in this case) which i actually intended to send.The sensor id and the headers is not added along with the request. Resulting in error response from the server stating that "Sensor Id cannot be empty". So, to test the server, i used an app in my PC to send request and worked well.
Below is what i need,
I need help in sending a body request to a specified URL using any library or API . Thanks in advance.