I am working on a NodeMCU board to post sensor data on my website service. Basically my project is about POST and GET requests both in single code.

I am succeeding in sending GET requests from my website. However, I just can't post data to my website.

To post I have 2 different links (asp services): one will post data and another will be used to get data same as transmitted by first link.

Here is my code, and also how to post 2 variables in http post request?

#include (ESP8266WiFi.h)
#include (ESP8266HTTPClient.h)

const char* ssid = "oppo";
const char* password = "11111111";    
int giSwitchNum = 1, buttonPin = 2;

void setup () {
  WiFi.begin(ssid, password);
  pinMode(D1, OUTPUT);
  pinMode(D2, INPUT);
  while (WiFi.status() != WL_CONNECTED) {
  giSwitchNum = 1;

void loop() {
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
    HTTPClient Post;
    Post.addHeader("operator", "text/plain");  
    String payload = Post.getString();
  • 1
    remove the 'operator' header and add a "Content-Type" header with value "application/x-www-form-urlencoded" – Juraj Mar 19 '18 at 19:02
void wifi_connect(){
  WiFi.mode(WIFI_OFF);        //Prevents reconnection issue (taking too long to connect)
  WiFi.mode(WIFI_STA);        //This line hides the viewing of ESP as wifi hotspot
  //WIFI_STA It's very important !!!!

  WiFi.begin(ssid, password);     //Connect to your WiFi router
  while(WiFi.status() != WL_CONNECTED){

  Serial.println("Wifi connected");
  Serial.print("IP address: ");
  • I think you may try "WiFi.mode(WIFI.STA)" – Dahd Oct 25 at 10:13

One way is to use the HTTPClient.h. It can be used for both HTTP and HTTPS communication(I prefer WiFiClientSecure.h for https though). Here is the link to the tutorial which shows the usage. The blog has all sorts of examples like GET Request, POST Request etc.


The code is pretty simple and easy to implement. Hope this helps.


client.print(String("POST ") + url + " HTTP/1.1\r\n" +
           "Host: " + host + "\r\n" +
           "Connection: close\r\n"+   
           "Content-Length: " + data.length() + "\r\n" +
           "Content-Type: application/json;charset=UTF-8\r\n\r\n"+
            data +"\r\n");

Try this as the format to send. Make a json data to send the variables.

  • no, i need to solution for only http post request. I've a one web service link for post data on server. which is post two variable values on server. if i'm try same code with one variable based data post link. then it's working well. but it's not work with two variable based link.(same code in both cases) – Jignesh narola Mar 19 '18 at 12:51
  • 1
    @Mrenosert, he uses HTTPClient. the esp8266 version. – Juraj Mar 20 '18 at 8:49
  • how should this help him? – Juraj Mar 20 '18 at 8:51
  • Yes, I know, even I have used it for a similar purpose and it is working. – Mreno sert Mar 20 '18 at 11:19
  • In the blog I mentioned above the entire working is completely written in a precise manner . – Mreno sert Mar 20 '18 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.