Sending HTTP body request to a specific URL

rammkumar
Posts: 4
Joined: Wed Apr 22, 2020 5:35 am

Sending HTTP body request to a specific URL

Postby rammkumar » 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]

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);
 
}
[/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.

ESP_Sprite
Posts: 9619
Joined: Thu Nov 26, 2015 4:08 am

Re: Sending HTTP body request to a specific URL

Postby ESP_Sprite » Wed Apr 22, 2020 8:10 am

Moving this to the Arduino subforum, as you seem to be asking about that.

User avatar
fasani
Posts: 195
Joined: Wed Jan 30, 2019 12:00 pm
Location: Barcelona
Contact:

Re: Sending HTTP body request to a specific URL

Postby fasani » Thu Apr 23, 2020 1:28 pm

I have some questions here, first of all I would like to ask with what server are you checking the logs, because as default in Apache you usually don't see this header requests. Do you see the header request in the log if you simulate it with Postman?

Secondly if I understand well, you plan to use Json, and "Sensorid:\"Sensor-1\"" is by no means valid Json.
{"Sensorid":"1"}

Would be. Now there are two ways to do this, one is to use httpclient and other one is to build the request yourself. In this project main.cpp you can see that I'm using both methods, maybe it's easier to spot something missing looking at my example:

https://github.com/martinberlin/eink-ca ... c/main.cpp

This also sending a variable per Post but in this case using WiFiClient. I use HttpClient only for easy requests. But your code looks good to me, that's why I ask how you are testing the headers, since I don't see the error in first sight.
epdiy collaborator | http://fasani.de Fan of Espressif MCUs and electronic design

rammkumar
Posts: 4
Joined: Wed Apr 22, 2020 5:35 am

Re: Sending HTTP body request to a specific URL

Postby rammkumar » Sat Apr 25, 2020 2:55 pm

@fasani Thank you so much for taking time to read through. Eventually, found out that the server we have set, expects data like this "Sensorid = 1". The difference being an equal sign instead of colon sign(:) in the data that we are posting to the server. Silly mistake. Apologies for wasting your time. Thanks anyway.

Who is online

Users browsing this forum: No registered users and 32 guests