ESP8266: HTTP GET Requests

The objective of this post is to explain how to perform GET Requests using the ESP8266.


Introduction

The objective of this post is to explain how to perform GET Requests using the ESP8266. If you prefer a video explanation, you can check my YouTube channel:

The tests were performed using a NodeMCU board, a very cheap and easy to use ESP8266 board.


Setup

First, we need do include some libraries. Naturally, we need the ESP8266WiFi library, which provides us the methods to connect to a WiFi network.

Then, we need the ESP8266HTTPClient library, which provides methods to send HTTP requests. The header file for the ESP8266HTTPClient library can be seen here.

#include <esp8266wifi.h>
#include <esp8266httpclient.h>

On the setup function, we connect to the WiFi Network. We also start a Serial connection for debugging. More details about how to connect to a WiFi network using the ESP8266 are explained in this previous post.

const char* ssid = "yourNetworkName";
const char* password = "yourNetworkPassword";

void setup () {

Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);
Serial.print("Connecting..");

}

}


The main code

The code for the request will be specified in the main loop function. First, we declare an object of class HTTPClient, which we will simply call http. This class provides the methods to create and send the HTTP request.

HTTPClient http;

After that, we call the begin method on the http object and pass the URL that we want to connect to and make the GET request. The destination website specified here implements a dummy REST API for testing and prototyping.

http.begin("http://jsonplaceholder.typicode.com/users/1");

Then, we send the request by calling the GET method on the http object. This method will return the status of the operation, which is important to store for error handling. If the value is greater than 0, then it’s a standard HTTP code. If the value is less than 0, then it’s a client error, related with the connection. All available error codes for this method are listed here.

int httpCode = http.GET();

So, if the code is greater than 0, we can get and print the response payload, by calling the getString method on the http object.

String payload = http.getString();
Serial.println(payload);

Finally, we call the end method. This is very important to close the TCP connection and thus free the resources.

http.end();

The final complete code is shown bellow. You just need to put your network name and network password.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "yourNetworkName";
const char* password = "yourNetworkPassword";

void setup () {

Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);
Serial.print("Connecting..");

}

}

void loop() {

if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

HTTPClient http;  //Declare an object of class HTTPClient

http.begin("http://jsonplaceholder.typicode.com/users/1");  //Specify request destination
int httpCode = http.GET();                                                                  //Send the request

if (httpCode > 0) { //Check the returning code

String payload = http.getString();   //Get the request response payload
Serial.println(payload);                     //Print the response payload

}

http.end();   //Close connection

}

delay(30000);    //Send a request every 30 seconds

}

The output of the program is shown in figure 1. The response corresponds to a JSON payload.

ESP8266 GET Request

Figure 1 – Output of the GET Request.

Finally, it’s important to take in consideration that the microcontroller has a limited amount of resources and thus it is not able to handle very large results. So, it is not expected that it will be used to make and process requests to sites that return a lot of information, as a typical browser would do.


Technical details

ESP8266 libraries: v2.3.0

154 Replies to “ESP8266: HTTP GET Requests”

  1. Hi, just a question, maybe not relate with this post.
    is it possible my UNO connect to Firebase via NodeMCU. My UNO act as a main controller, and i wan my nodemcu act as just a gateway for UNO to connect to Wifi. And i wan the code is on the UNO instead of on nodemcu.
    Hopefully, will get some idea from you. Thanks you. =D

    1. Hi!

      I don’t know which protocols Firebase supports, but I’m assuming it is one that can be used on the ESP (such as HTTP).

      If so, you should be able to what you mentioned.

      You need to send the data you want from the uno to the ESP and then the ESP should build the HTTP request from that data and send it to firebase.

      The easiest way to make the uno and the NodeMCU talk is by serial communication.

      Note however that the ESP works with 3.3 V and Uno with 5 V (at least the older versions) so you will need to perform level conversion.

      Nonetheless you will need to program the NodeMCU to do it in the most efficient way.

      The only alternative for not programming the NodeMCU is using the ESP AT commands, but I would recommend against it since they are not very reliable or flexible (at least they weren’t a couple of time ago).

      Bottom line, my recommendation is that you take a while to learn how to program the ESP8266 and think about using only it.

      The ESP8266 if far more powerful than the Uno and if you develop a program to it you will not need to worry about the level conversion and converting from serial to HTTP.

      Hope it helps,
      Nuno Santos

  2. Hi, just a question, maybe not relate with this post.
    is it possible my UNO connect to Firebase via NodeMCU. My UNO act as a main controller, and i wan my nodemcu act as just a gateway for UNO to connect to Wifi. And i wan the code is on the UNO instead of on nodemcu.
    Hopefully, will get some idea from you. Thanks you. =D

    1. Hi!
      I don’t know which protocols Firebase supports, but I’m assuming it is one that can be used on the ESP (such as HTTP).
      If so, you should be able to what you mentioned.
      You need to send the data you want from the uno to the ESP and then the ESP should build the HTTP request from that data and send it to firebase.
      The easiest way to make the uno and the NodeMCU talk is by serial communication.
      Note however that the ESP works with 3.3 V and Uno with 5 V (at least the older versions) so you will need to perform level conversion.
      Nonetheless you will need to program the NodeMCU to do it in the most efficient way.
      The only alternative for not programming the NodeMCU is using the ESP AT commands, but I would recommend against it since they are not very reliable or flexible (at least they weren’t a couple of time ago).
      Bottom line, my recommendation is that you take a while to learn how to program the ESP8266 and think about using only it.
      The ESP8266 if far more powerful than the Uno and if you develop a program to it you will not need to worry about the level conversion and converting from serial to HTTP.
      Hope it helps,
      Nuno Santos

  3. Hey,
    I’m using the esp8266 to get data from an API that the dutch railways provide, they return some xml data, however I can connect to the api. I am using the ESP8266HTTPClient library but the http.connected() is always 0. How do I get it to be a usable number? And how can I process XML data with the ESP8266?
    Thanks in advance for your help!

    1. Hi!

      In the code from this tutorial I’m not making any call to the http connected method. Are you using a modified version?

      Nonetheless, that method should return true if the tcp connection is ok and false otherwise, so you may be experiencing some problems with the connection.

      Are you able to use the same ESP8266 code to reach other websites with success? And are you able to reach the railway api and get the response with other tool, such as Postman or cURL?

      Regarding the XML processing, I’m not aware of any good parser for the ESP8266 or the Arduino. Only know for JSON format.

      Best regards,
      Nuno Santos

      1. Thanks for replying!
        I think I have some new ideas and I will try a few new things. Thanks for telling me about Postman and cURL, I will definitely try them and maybe use them in future projects.

  4. Hey,
    I’m using the esp8266 to get data from an API that the dutch railways provide, they return some xml data, however I can connect to the api. I am using the ESP8266HTTPClient library but the http.connected() is always 0. How do I get it to be a usable number? And how can I process XML data with the ESP8266?
    Thanks in advance for your help!

    1. Hi!
      In the code from this tutorial I’m not making any call to the http connected method. Are you using a modified version?
      Nonetheless, that method should return true if the tcp connection is ok and false otherwise, so you may be experiencing some problems with the connection.
      Are you able to use the same ESP8266 code to reach other websites with success? And are you able to reach the railway api and get the response with other tool, such as Postman or cURL?
      Regarding the XML processing, I’m not aware of any good parser for the ESP8266 or the Arduino. Only know for JSON format.
      Best regards,
      Nuno Santos

      1. Thanks for replying!
        I think I have some new ideas and I will try a few new things. Thanks for telling me about Postman and cURL, I will definitely try them and maybe use them in future projects.

  5. Hey,
    I’m using the esp8266 to get data from an API that the dutch railways provide, they return some xml data, however I can connect to the api. I am using the ESP8266HTTPClient library but the http.connected() is always 0. How do I get it to be a usable number? And how can I process XML data with the ESP8266?
    Thanks in advance for your help!

  6. Hey,
    I’m using the esp8266 to get data from an API that the dutch railways provide, they return some xml data, however I can connect to the api. I am using the ESP8266HTTPClient library but the http.connected() is always 0. How do I get it to be a usable number? And how can I process XML data with the ESP8266?
    Thanks in advance for your help!

    1. Hi!

      Well, you can do some sort of polling mechanism where you use an HTTP request to ask the server if you should connect your relay or not.

      Nonetheless, that would not be the most efficient approach.

      A better alternative would be setting a server on your ESP, which would in that case receive requests from a client to control the relay.

      You can check here how to get started:
      https://techtutorialsx.com/2016/10/03/esp8266-setting-a-simple-http-webserver/

      Note however that you will only be able to reach the server from inside your local network, unless you portforward your router, which may be a little bit more complicated if you are not used to such procedure, and also expose your network to some dangers.

      If you need to send commands to your relay from outside the local network, then a safer way may be opening a socket connection to the server and receive the commands from that interface.

      But it’s a matter of deciding which plays best for the requirements of your application.

      Best regards,
      Nuno Santos

    1. Hi!
      Well, you can do some sort of polling mechanism where you use an HTTP request to ask the server if you should connect your relay or not.
      Nonetheless, that would not be the most efficient approach.
      A better alternative would be setting a server on your ESP, which would in that case receive requests from a client to control the relay.
      You can check here how to get started:
      https://techtutorialsx.com/2016/10/03/esp8266-setting-a-simple-http-webserver/
      Note however that you will only be able to reach the server from inside your local network, unless you portforward your router, which may be a little bit more complicated if you are not used to such procedure, and also expose your network to some dangers.
      If you need to send commands to your relay from outside the local network, then a safer way may be opening a socket connection to the server and receive the commands from that interface.
      But it’s a matter of deciding which plays best for the requirements of your application.
      Best regards,
      Nuno Santos

    1. Hi!

      What do you mean by receiving the 1 in the URL? Is your ESP8266 working as a webserver (thus receiving HTTP requests) or are you making a request from your ESP8266 to a URL?

      If you are making a request, then you would receive that 1 in the answer of the request.

      In that case, you would need to parse the result to an integer and check if it has the value 1.

      If it does, simply activate the pin by calling the digital write function:
      https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/

      As indicated in that page, don’t forget to initialize the pin as Output before using it.

      Hope this helps.

      Best regards,
      Nuno Santos

    1. Hi!
      What do you mean by receiving the 1 in the URL? Is your ESP8266 working as a webserver (thus receiving HTTP requests) or are you making a request from your ESP8266 to a URL?
      If you are making a request, then you would receive that 1 in the answer of the request.
      In that case, you would need to parse the result to an integer and check if it has the value 1.
      If it does, simply activate the pin by calling the digital write function:
      https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/
      As indicated in that page, don’t forget to initialize the pin as Output before using it.
      Hope this helps.
      Best regards,
      Nuno Santos

  7. hi antephar
    i need a help
    i am trying to form a cluster with nodemcu modules, where one nodemcu act as server and other nodemcus act as clients. my problem here is i want to request the data from the particular client by IP address from the server. Is it possible by the http.get() method.
    thank u

    1. Hi!

      I’m not sure if I understood the architecture correctly, but you will have one NodeMCU as server and the remaining as clients, right?

      If so, I think it should be the clients asking data to the server and not the other way around. At least for a typical approach πŸ™‚

      Why do you need to have the server contacting the clients? In that case, maybe you should have the other NodeMCUs working as servers and the one that is asking working as client.

      Does this make sense for your architecture?

      Best regards,
      Nuno Santos

  8. hi antephar
    i need a help
    i am trying to form a cluster with nodemcu modules, where one nodemcu act as server and other nodemcus act as clients. my problem here is i want to request the data from the particular client by IP address from the server. Is it possible by the http.get() method.
    thank u

    1. Hi!
      I’m not sure if I understood the architecture correctly, but you will have one NodeMCU as server and the remaining as clients, right?
      If so, I think it should be the clients asking data to the server and not the other way around. At least for a typical approach πŸ™‚
      Why do you need to have the server contacting the clients? In that case, maybe you should have the other NodeMCUs working as servers and the one that is asking working as client.
      Does this make sense for your architecture?
      Best regards,
      Nuno Santos

    1. Hi!

      Which endpoint are you trying to reach? Can you also reach it from a web browser?

      Also, is it HTTP or HTTPS? This example doesn’t work for HTTPs.

      Best regards,
      Nuno Santos

    1. Hi!
      Which endpoint are you trying to reach? Can you also reach it from a web browser?
      Also, is it HTTP or HTTPS? This example doesn’t work for HTTPs.
      Best regards,
      Nuno Santos

  9. Could you help me?
    I’m working with two Nodemcu. There is a server (access point) and a client. I got how to send requests from client to server, but I don’t know how to get those requests using the server.

    1. Hi!

      What do you mean by getting them in the server? Which library are you using?

      The typical implementations of webservers on the esp8266 / esp32 use the concept of routes.

      Whenever you receive a request on a route, it executes a route handling function, so is there that you handle the request.

      Then on those routes there are functions to access the parameters of the request.

      The parameters available will pretty much depend on the type of request (POST, GET, etc..), what you send on the query string etc..

      But let me know a little bit more about what you are doing so I can try to help πŸ™‚

      Best regards,
      Nuno Santos

  10. Could you help me?
    I’m working with two Nodemcu. There is a server (access point) and a client. I got how to send requests from client to server, but I don’t know how to get those requests using the server.

    1. Hi!
      What do you mean by getting them in the server? Which library are you using?
      The typical implementations of webservers on the esp8266 / esp32 use the concept of routes.
      Whenever you receive a request on a route, it executes a route handling function, so is there that you handle the request.
      Then on those routes there are functions to access the parameters of the request.
      The parameters available will pretty much depend on the type of request (POST, GET, etc..), what you send on the query string etc..
      But let me know a little bit more about what you are doing so I can try to help πŸ™‚
      Best regards,
      Nuno Santos

  11. Hi this might sound silly, but im always getting http code 302 for a particular rest API(accessible without logging in ). Also, postman app gives me response as 200 along with payload . http://jsonplaceholder.typicode.com/users/1 gives perfect response with nodeMCU.
    thus im not able to parse contents,

    Please help.!!!
    im just passing some headers along with the get request
    snippet:
    http.begin(“my rest url”); //Specify request destination
    int httpCode = http.GET(); //Send the request
    Serial.println(httpCode); // returns302
    if (httpCode > 0) { //Check the returning code

    String payload = http.getString(); //Get the request response payload
    Serial.println(payload); //Print the response payload

    }

    http.end();

    BTW, it returns -1 when any any other url is used ;(

    1. a bit modification:

      snippet:
      http.begin(“my rest url”); //Specify request destination does not have www including http
      http.addHeader(“Accept”, “application/json”);
      http.addHeader(“Content-Typez”, “application/json”);
      http.addHeader(“location”, “same URL including https);
      int httpCode = http.GET(); //Send the request
      Serial.println(httpCode); // returns302
      if (httpCode > 0) { //Check the returning code

      String payload = http.getString(); //Get the request response payload
      Serial.println(payload); //Print the response payload

      }

      http.end();

      1. Don’t forget the http://
        You should use http://yourURL

        Note also that content-type header as a typo (a Z at the end that should not be there)

        Does your endpoint work if you don’t specify any headers?

        Best regards,
        Nuno Santos

    2. Hi! That is very weird.

      When using postman, did you also send the same headers?

      Is your REST URL HTTP or HTTPS? Also, does it end with a / ? If so, are you including it? I’ve seen people having some problems with it because of not including the ending /.

      Other than that it is very weird since 302 is redirection code.

      Is the REST API public or hosted by you?

      Best regards,
      Nuno Santos

  12. Hi this might sound silly, but im always getting http code 302 for a particular rest API(accessible without logging in ). Also, postman app gives me response as 200 along with payload . http://jsonplaceholder.typicode.com/users/1 gives perfect response with nodeMCU.
    thus im not able to parse contents,
    Please help.!!!
    im just passing some headers along with the get request
    snippet:
    http.begin(“my rest url”); //Specify request destination
    int httpCode = http.GET(); //Send the request
    Serial.println(httpCode); // returns302
    if (httpCode > 0) { //Check the returning code
    String payload = http.getString(); //Get the request response payload
    Serial.println(payload); //Print the response payload
    }
    http.end();
    BTW, it returns -1 when any any other url is used ;(

    1. a bit modification:
      snippet:
      http.begin(“my rest url”); //Specify request destination does not have www including http
      http.addHeader(“Accept”, “application/json”);
      http.addHeader(“Content-Typez”, “application/json”);
      http.addHeader(“location”, “same URL including https);
      int httpCode = http.GET(); //Send the request
      Serial.println(httpCode); // returns302
      if (httpCode > 0) { //Check the returning code
      String payload = http.getString(); //Get the request response payload
      Serial.println(payload); //Print the response payload
      }
      http.end();

      1. Don’t forget the http://
        You should use http://yourURL
        Note also that content-type header as a typo (a Z at the end that should not be there)
        Does your endpoint work if you don’t specify any headers?
        Best regards,
        Nuno Santos

    2. Hi! That is very weird.
      When using postman, did you also send the same headers?
      Is your REST URL HTTP or HTTPS? Also, does it end with a / ? If so, are you including it? I’ve seen people having some problems with it because of not including the ending /.
      Other than that it is very weird since 302 is redirection code.
      Is the REST API public or hosted by you?
      Best regards,
      Nuno Santos

  13. Hey, thanks for this, it was very helpful! I’m using a powerbank to power the ESP8266 and I want to reduce electricity consumption, so that I can go longer without touching the setup. Would using deep sleep (or some other method) result in longer battery time, compared to delay function? Thanks again.

    1. Hi!

      You’re welcome πŸ™‚

      Theoretically it should reduce the power consumption if you go to deep sleep, as long as you are not doing very frequent changes between normal operation and deep sleep.

      This is because changing between those two modes of operation usually involves some power consumption overhead that only compensates if the device stays in low power mode for a while.

      This is theoretical and what typically happens generically in microcontrollers.

      Nonetheless, I never had the chance to analyze the specifics of the ESP8266 regarding low power modes, so I’m not sure what these thresholds are.

      My recommendation is that you take a look at the ESP8266 datasheet to check if there is some information about the deep sleep mode of operation, its consumption and the power consumption to change between modes.

      Hope this helps getting you in the right track.

      Best regards,
      Nuno Santos

  14. Hey, thanks for this, it was very helpful! I’m using a powerbank to power the ESP8266 and I want to reduce electricity consumption, so that I can go longer without touching the setup. Would using deep sleep (or some other method) result in longer battery time, compared to delay function? Thanks again.

    1. Hi!
      You’re welcome πŸ™‚
      Theoretically it should reduce the power consumption if you go to deep sleep, as long as you are not doing very frequent changes between normal operation and deep sleep.
      This is because changing between those two modes of operation usually involves some power consumption overhead that only compensates if the device stays in low power mode for a while.
      This is theoretical and what typically happens generically in microcontrollers.
      Nonetheless, I never had the chance to analyze the specifics of the ESP8266 regarding low power modes, so I’m not sure what these thresholds are.
      My recommendation is that you take a look at the ESP8266 datasheet to check if there is some information about the deep sleep mode of operation, its consumption and the power consumption to change between modes.
      Hope this helps getting you in the right track.
      Best regards,
      Nuno Santos

  15. hello antepher, thanks for making this tutorial. i want to ask, can i use the value from the json as my pinout value ? thanks

  16. hello antepher, thanks for making this tutorial. i want to ask, can i use the value from the json as my pinout value ? thanks

  17. antepher thanks for the tutorial! I’m looking for a way to receive (and process) data from the server response line – not the payload. Especial I’m intretsted in the time and date stamp from the server respond.
    Thanks

    1. Hi!

      You’re welcome and thank you very much for the feedback πŸ™‚

      What do you mean by the server response line? Something like the response headers?

      Best regards,
      Nuno Santos

      1. yes exacly, The line that contains time and date info. With this info I like to set the system time An example ho to do this is welcomed πŸ˜‰
        Thank you!

          1. Hi Nuno,
            thats exacly what I was looking for. Thank you so much!
            If you point me to the documentation of ESP8266HTTPClient documentation next time I’ll first look at before asking stupid questions
            ——————–
            Connecting..
            Connected to WiFi Network
            Sat, 09 Jun 2018 06:31:08 GMT
            cloudflare
            Sat, 09 Jun 2018 06:31:08 GMT
            cloudflare
            ——————–
            Thanks
            Jochen

            1. Hi!

              Awesome, you’re welcome πŸ™‚

              It was not a stupid question at all. In fact I hadn’t yet played with the headers of the response, so it was an interesting exercise to figure it out and I think it will be useful for more people πŸ™‚

              I’m not sure if there is some “official” documentation about the HTTP Client. The only place that has some structured documentation about the libraries that I’m aware of is the following:
              https://arduino-esp8266.readthedocs.io/en/latest/index.html

              Nonetheless, I did not find anything about the HTTPClient :/

              What I usually do to check the API of some class if actually going to the header file and check the available methods. Sometimes they are straightforward to use, but others it requires some digging on wikis, the github issues and the implementation files.

              If you want to take a look, here is the header file of the HTTP Client:
              https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h

              I wish you good luck in your project πŸ™‚

              Best regards,
              Nuno Santos

          2. Hi Nuno,
            I’m facing the problen to send a request to a server that insists of pfs connection set up. As this behavior will become more prominent it’s not just me beeing confronted I believe.
            Thank you

            1. Hi!

              When you mean pfs, are you refering to perfect forward secrecy?

              If so, at the time I was more actively working with the ESP8266, HTTPS functionalities were really limited and I’m not sure if perfect forward secrecy was ever supported.

              To be honest, I’m not sure if things have evolved much since then, but I would recommend you to take a look at the ESP32 for more robust security functionalities.

              The ESP32 is far more powerful and it is able to handle HTTPS very well. But even with the ESP32, I’m not sure if it can support perfect forward secrecy, so you would need to do some investigation or ask around the forums and git hub pages.

              But an easy way to test is to make an HTTPS request to a server that uses PFS and check if everything works.

              If I come across some information or examples regarding pfs support, I will make sure to share πŸ™‚

              Best regards,
              Nuno Santos

              1. Hi Nuno,
                I’ve send this request to Espresif: “As perfect forward secrecy, short pfs, becomes more prominent/importand I wonder if there is a pfs implementation for the famous ESP32”
                Looking forward for an answer …

              2. Hi!

                Please let us know if they give back some answer, I’m also curious about what their answer will be since this is indeed something very useful to have available.

                Hopefully there is support πŸ™‚

                Best regards,
                Nuno Santos

              3. It’s a shame, no answer yet – no positve nor negativ – jusst silenc :-((
                As my english isn’t proper and my knowledge of internet protocols is poor maybe somone can step into this request and forward it to the right community. As I said befor it’s an upcomming demand.
                Thank you for your great support. All the best
                Mhz

              4. Hi,

                I’m sad to hear that you haven’t yet got an answer from Espressif, it would be interesting to know their answer on this.

                Nonetheless, I think it is probable that you get some answers in forums or on the github pages of ESP8266 Projects. Here is a list of interesting places where you can ask around:
                – ESP8266 forum: https://www.esp8266.com/
                – ESP8266 Arduino core github page: https://github.com/esp8266/Arduino/issues

                Nonetheless, I would also ask around the ESP32 forum, since it is more probable that this support for pfs gets to the ESP32:
                – Forum: https://esp32.com/
                – Arduino core Github page: https://github.com/espressif/arduino-esp32
                – IDF Github Page: https://github.com/espressif/esp-idf

                Don’t worry about your English, I’m not a native speaker myself and never worry about that when asking about stuff in technical forums πŸ™‚

                You’re welcome, and thanks for the feedback πŸ™‚

                Best regards,
                Nuno Santos

  18. antepher thanks for the tutorial! I’m looking for a way to receive (and process) data from the server response line – not the payload. Especial I’m intretsted in the time and date stamp from the server respond.
    Thanks

    1. Hi!
      You’re welcome and thank you very much for the feedback πŸ™‚
      What do you mean by the server response line? Something like the response headers?
      Best regards,
      Nuno Santos

      1. yes exacly, The line that contains time and date info. With this info I like to set the system time An example ho to do this is welcomed πŸ˜‰
        Thank you!

          1. Hi Nuno,
            thats exacly what I was looking for. Thank you so much!
            If you point me to the documentation of ESP8266HTTPClient documentation next time I’ll first look at before asking stupid questions
            ——————–
            Connecting..
            Connected to WiFi Network
            Sat, 09 Jun 2018 06:31:08 GMT
            cloudflare
            Sat, 09 Jun 2018 06:31:08 GMT
            cloudflare
            ——————–
            Thanks
            Jochen

            1. Hi!
              Awesome, you’re welcome πŸ™‚
              It was not a stupid question at all. In fact I hadn’t yet played with the headers of the response, so it was an interesting exercise to figure it out and I think it will be useful for more people πŸ™‚
              I’m not sure if there is some “official” documentation about the HTTP Client. The only place that has some structured documentation about the libraries that I’m aware of is the following:
              https://arduino-esp8266.readthedocs.io/en/latest/index.html
              Nonetheless, I did not find anything about the HTTPClient :/
              What I usually do to check the API of some class if actually going to the header file and check the available methods. Sometimes they are straightforward to use, but others it requires some digging on wikis, the github issues and the implementation files.
              If you want to take a look, here is the header file of the HTTP Client:
              https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h
              I wish you good luck in your project πŸ™‚
              Best regards,
              Nuno Santos

          2. Hi Nuno,
            I’m facing the problen to send a request to a server that insists of pfs connection set up. As this behavior will become more prominent it’s not just me beeing confronted I believe.
            Thank you

            1. Hi!
              When you mean pfs, are you refering to perfect forward secrecy?
              If so, at the time I was more actively working with the ESP8266, HTTPS functionalities were really limited and I’m not sure if perfect forward secrecy was ever supported.
              To be honest, I’m not sure if things have evolved much since then, but I would recommend you to take a look at the ESP32 for more robust security functionalities.
              The ESP32 is far more powerful and it is able to handle HTTPS very well. But even with the ESP32, I’m not sure if it can support perfect forward secrecy, so you would need to do some investigation or ask around the forums and git hub pages.
              But an easy way to test is to make an HTTPS request to a server that uses PFS and check if everything works.
              If I come across some information or examples regarding pfs support, I will make sure to share πŸ™‚
              Best regards,
              Nuno Santos

              1. Hi Nuno,
                I’ve send this request to Espresif: “As perfect forward secrecy, short pfs, becomes more prominent/importand I wonder if there is a pfs implementation for the famous ESP32”
                Looking forward for an answer …

              2. Hi!
                Please let us know if they give back some answer, I’m also curious about what their answer will be since this is indeed something very useful to have available.
                Hopefully there is support πŸ™‚
                Best regards,
                Nuno Santos

              3. It’s a shame, no answer yet – no positve nor negativ – jusst silenc :-((
                As my english isn’t proper and my knowledge of internet protocols is poor maybe somone can step into this request and forward it to the right community. As I said befor it’s an upcomming demand.
                Thank you for your great support. All the best
                Mhz

              4. Hi,
                I’m sad to hear that you haven’t yet got an answer from Espressif, it would be interesting to know their answer on this.
                Nonetheless, I think it is probable that you get some answers in forums or on the github pages of ESP8266 Projects. Here is a list of interesting places where you can ask around:
                – ESP8266 forum: https://www.esp8266.com/
                – ESP8266 Arduino core github page: https://github.com/esp8266/Arduino/issues
                Nonetheless, I would also ask around the ESP32 forum, since it is more probable that this support for pfs gets to the ESP32:
                – Forum: https://esp32.com/
                – Arduino core Github page: https://github.com/espressif/arduino-esp32
                – IDF Github Page: https://github.com/espressif/esp-idf
                Don’t worry about your English, I’m not a native speaker myself and never worry about that when asking about stuff in technical forums πŸ™‚
                You’re welcome, and thanks for the feedback πŸ™‚
                Best regards,
                Nuno Santos

  19. hello sir ,
    iam trying to send the url to the mysql database. but using below sketch not sending ,where i did mistake please help.

    #include
    #include

    void setup() {

    Serial.begin(115200); //Serial connection
    WiFi.begin(“iota_telecom”, “iota160616”); //WiFi connection

    while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion

    delay(500);
    Serial.println(“Waiting for connection”);

    }

    }

    void loop() {

    if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status

    HTTPClient http; //Declare object of class HTTPClient

    http.begin(“http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //Specify request destination
    http.addHeader(“Content-Type”, “text/plain”); //Specify content-type header

    int httpCode = http.POST(“http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //Send the request
    String payload = http.getString(); //Get the response payload

    Serial.println(httpCode); //Print HTTP return code
    Serial.println(payload); //Print request response payload

    http.end(); //Close connection

    }else{

    Serial.println(“Error in WiFi connection”);

    }

    delay(30000); //Send a request every 30 seconds

    }

    serial monitor:

    Waiting for connection
    state: 2 -> 0 (2)
    reconnect
    scandone
    state: 0 -> 2 (b0)
    Waiting for connection
    state: 2 -> 3 (0)
    state: 3 -> 5 (10)
    add 0
    aid 2
    cnt
    Waiting for connection
    Waiting for connection
    Waiting for connection

    connected with iota_telecom, channel 6
    dhcp client start…
    Waiting for connection
    ip:10.132.39.81,mask:255.255.0.0,gw:10.132.0.1
    Waiting for connection
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] connected to 10.132.16.241:8080
    [HTTP-Client] sending request header
    —–
    POST /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45 HTTP/1.1
    Host: 10.132.16.241:8080
    User-Agent: ESP8266HTTPClient
    Connection: close
    Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
    Content-Type: text/plain
    Content-Length: 136

    —–
    [HTTP-Client][returnError] error(-11): read Timeout
    [HTTP-Client][returnError] tcp stop
    [HTTP-Client][returnError] error(-4): not connected
    -11

    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1

    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1

    I’m using nodeMCU(esp_12E)

  20. hello sir ,
    iam trying to send the url to the mysql database. but using below sketch not sending ,where i did mistake please help.
    #include
    #include
    void setup() {
    Serial.begin(115200); //Serial connection
    WiFi.begin(“iota_telecom”, “iota160616”); //WiFi connection
    while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion
    delay(500);
    Serial.println(“Waiting for connection”);
    }
    }
    void loop() {
    if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
    HTTPClient http; //Declare object of class HTTPClient
    http.begin(“http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //Specify request destination
    http.addHeader(“Content-Type”, “text/plain”); //Specify content-type header
    int httpCode = http.POST(“http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //Send the request
    String payload = http.getString(); //Get the response payload
    Serial.println(httpCode); //Print HTTP return code
    Serial.println(payload); //Print request response payload
    http.end(); //Close connection
    }else{
    Serial.println(“Error in WiFi connection”);
    }
    delay(30000); //Send a request every 30 seconds
    }
    serial monitor:
    Waiting for connection
    state: 2 -> 0 (2)
    reconnect
    scandone
    state: 0 -> 2 (b0)
    Waiting for connection
    state: 2 -> 3 (0)
    state: 3 -> 5 (10)
    add 0
    aid 2
    cnt
    Waiting for connection
    Waiting for connection
    Waiting for connection
    connected with iota_telecom, channel 6
    dhcp client start…
    Waiting for connection
    ip:10.132.39.81,mask:255.255.0.0,gw:10.132.0.1
    Waiting for connection
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] connected to 10.132.16.241:8080
    [HTTP-Client] sending request header
    —–
    POST /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45 HTTP/1.1
    Host: 10.132.16.241:8080
    User-Agent: ESP8266HTTPClient
    Connection: close
    Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
    Content-Type: text/plain
    Content-Length: 136
    —–
    [HTTP-Client][returnError] error(-11): read Timeout
    [HTTP-Client][returnError] tcp stop
    [HTTP-Client][returnError] error(-4): not connected
    -11
    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1
    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1
    I’m using nodeMCU(esp_12E)

  21. sorry sir ,my first post was not added libraries ,please help me where i did the mistake.

    #include
    #include

    void setup() {

    Serial.begin(115200); //Serial connection
    WiFi.begin(“iota_telecom”, “iota160616”); //WiFi connection

    while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion

    delay(500);
    Serial.println(“Waiting for connection”);

    }

    }

    void loop() {

    if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status

    HTTPClient http; //Declare object of class HTTPClient

    http.begin(“http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //Specify request destination
    http.addHeader(“Content-Type”, “text/plain”); //Specify content-type header

    int httpCode = http.POST(“http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //Send the request
    String payload = http.getString(); //Get the response payload

    Serial.println(httpCode); //Print HTTP return code
    Serial.println(payload); //Print request response payload

    http.end(); //Close connection

    }else{

    Serial.println(“Error in WiFi connection”);

    }

    delay(30000); //Send a request every 30 seconds

    }

    serial monitor:

    Waiting for connection
    state: 2 -> 0 (2)
    reconnect
    scandone
    state: 0 -> 2 (b0)
    Waiting for connection
    state: 2 -> 3 (0)
    state: 3 -> 5 (10)
    add 0
    aid 2
    cnt
    Waiting for connection
    Waiting for connection
    Waiting for connection

    connected with iota_telecom, channel 6
    dhcp client start…
    Waiting for connection
    ip:10.132.39.81,mask:255.255.0.0,gw:10.132.0.1
    Waiting for connection
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] connected to 10.132.16.241:8080
    [HTTP-Client] sending request header
    —–
    POST /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45 HTTP/1.1
    Host: 10.132.16.241:8080
    User-Agent: ESP8266HTTPClient
    Connection: close
    Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
    Content-Type: text/plain
    Content-Length: 136

    —–
    [HTTP-Client][returnError] error(-11): read Timeout
    [HTTP-Client][returnError] tcp stop
    [HTTP-Client][returnError] error(-4): not connected
    -11

    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1

    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1
    thank you.

    1. Hi!

      Unfortunately I’m not aware how these connections to the mysql database work, so I cannot be of much assistance.

      Nonetheless, I can recommend a couple of things to try to help you debugging:
      – Can you connect to the database and send the request using another tool, such as Postman? Postman is very easy to use and helps testing HTTP requests. So if it is also not working with postman, then it is not an ESP related problem.
      – Are you sure the database accepts plain HTTP requests? Or does it work over HTTPS (note the s, from secure)?
      – Why are you sending the URL as body of your HTTP request? You should just put the content you want to send to the server in the body of your POST request.

      Hope this helps.

      Best regards,
      Nuno Santos

  22. sorry sir ,my first post was not added libraries ,please help me where i did the mistake.
    #include
    #include
    void setup() {
    Serial.begin(115200); //Serial connection
    WiFi.begin(“iota_telecom”, “iota160616”); //WiFi connection
    while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion
    delay(500);
    Serial.println(“Waiting for connection”);
    }
    }
    void loop() {
    if(WiFi.status()== WL_CONNECTED){ //Check WiFi connection status
    HTTPClient http; //Declare object of class HTTPClient
    http.begin(“http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //Specify request destination
    http.addHeader(“Content-Type”, “text/plain”); //Specify content-type header
    int httpCode = http.POST(“http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45”); //Send the request
    String payload = http.getString(); //Get the response payload
    Serial.println(httpCode); //Print HTTP return code
    Serial.println(payload); //Print request response payload
    http.end(); //Close connection
    }else{
    Serial.println(“Error in WiFi connection”);
    }
    delay(30000); //Send a request every 30 seconds
    }
    serial monitor:
    Waiting for connection
    state: 2 -> 0 (2)
    reconnect
    scandone
    state: 0 -> 2 (b0)
    Waiting for connection
    state: 2 -> 3 (0)
    state: 3 -> 5 (10)
    add 0
    aid 2
    cnt
    Waiting for connection
    Waiting for connection
    Waiting for connection
    connected with iota_telecom, channel 6
    dhcp client start…
    Waiting for connection
    ip:10.132.39.81,mask:255.255.0.0,gw:10.132.0.1
    Waiting for connection
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] connected to 10.132.16.241:8080
    [HTTP-Client] sending request header
    —–
    POST /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45 HTTP/1.1
    Host: 10.132.16.241:8080
    User-Agent: ESP8266HTTPClient
    Connection: close
    Accept-Encoding: identity;q=1,chunked;q=0.1,*;q=0
    Content-Type: text/plain
    Content-Length: 136
    —–
    [HTTP-Client][returnError] error(-11): read Timeout
    [HTTP-Client][returnError] tcp stop
    [HTTP-Client][returnError] error(-4): not connected
    -11
    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1
    [HTTP-Client][end] tcp is closed
    [HTTP-Client][begin] url: http://10.132.16.241:8080/HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client][begin] host: 10.132.16.241 port: 8080 url: /HTTPHITURL/RequestHandler?p1=300&p2=78&p3=500&p4=600&p5=500&p6=iota66&p7=iota77&p8=iota83&p9=iota84&p10=iota45
    [HTTP-Client] failed connect to 10.132.16.241:8080
    [HTTP-Client][returnError] error(-1): connection refused
    [HTTP-Client][returnError] error(-4): not connected
    -1
    thank you.

    1. Hi!
      Unfortunately I’m not aware how these connections to the mysql database work, so I cannot be of much assistance.
      Nonetheless, I can recommend a couple of things to try to help you debugging:
      – Can you connect to the database and send the request using another tool, such as Postman? Postman is very easy to use and helps testing HTTP requests. So if it is also not working with postman, then it is not an ESP related problem.
      – Are you sure the database accepts plain HTTP requests? Or does it work over HTTPS (note the s, from secure)?
      – Why are you sending the URL as body of your HTTP request? You should just put the content you want to send to the server in the body of your POST request.
      Hope this helps.
      Best regards,
      Nuno Santos

  23. hello sir how are you. this post is very useful but i have some problems like
    what is meant by http.get()
    and what is meant by http.getstring()
    please resolve this issues

    1. Hi!

      Thanks for the feedback πŸ™‚

      As explained in the tutorial, http.GET() is used to perform the actual request and http.getString() is used to get the response returned by the server.

      What is your concrete doubt about these methods?

      Best regards,
      Nuno Santos

  24. hello sir how are you. this post is very useful but i have some problems like
    what is meant by http.get()
    and what is meant by http.getstring()
    please resolve this issues

    1. Hi!
      Thanks for the feedback πŸ™‚
      As explained in the tutorial, http.GET() is used to perform the actual request and http.getString() is used to get the response returned by the server.
      What is your concrete doubt about these methods?
      Best regards,
      Nuno Santos

    1. Hi!

      You can use the addHeader method of the HTTPClient class. Below is an example in the context of making a POST request, but it works the same way for GET:
      https://techtutorialsx.com/2016/07/21/esp8266-post-requests/

      The first argument is the name of the header and the second is the value.

      But please note that this tutorial uses HTTP, which means that the request headers are sent in plain text to the server.

      HTTP is not encrypted, HTTPS is (note the additional S at the end, from Secure).

      So this basically means that sending an API key is not secure and an attacker can easily intercept the request and steal it.

      Hope this helps πŸ™‚

      Best regards,
      Nuno Santos

    1. Hi!
      You can use the addHeader method of the HTTPClient class. Below is an example in the context of making a POST request, but it works the same way for GET:
      https://techtutorialsx.com/2016/07/21/esp8266-post-requests/
      The first argument is the name of the header and the second is the value.
      But please note that this tutorial uses HTTP, which means that the request headers are sent in plain text to the server.
      HTTP is not encrypted, HTTPS is (note the additional S at the end, from Secure).
      So this basically means that sending an API key is not secure and an attacker can easily intercept the request and steal it.
      Hope this helps πŸ™‚
      Best regards,
      Nuno Santos

  25. Hi there, thanks for the tutorial! Do you know if there’s a way of doing this with the Wifi Client library? Basically I am running a local server with a page connected to a remote server. The remote server returns a json file. However I am not able to retrieve in the code. I’ll post here just my void loop() since it’s a bit long (my full code is here – https://forum.arduino.cc/index.php?topic=559170.new#new – and I’d be really greatful if you could answer here since I’m not getting much help over there). Thanks!!

    void loop(){
    WiFiClient client = server.available();
    if (!client) {
    return;
    }

    // Wait until the client sends some data
    SPL(“new client”);
    while(!client.available()){
    delay(1);
    }

    //this doesn’t print the payload (which sould be the json. It works well in the browser)
    byte b;
    while (client.readBytes(&b, 1) > 0) {
    Serial.write(b);*
    }

    client.flush();

    // Prepare the response
    String s = “HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n”;
    s += page; //the html page hosted on the local server and which connects to the remote one
    s += “\n”;

    // Send the response to the client
    client.print(s);
    }

    1. Hi!

      You’re welcome πŸ™‚

      Well that’s a complex architecture, may I ask why can’t you directly fetch the json from the remote server?

      When you mention that you are hosting a server locally, are you hosting it on a computer on the same network of the ESP8266, or are you actually hosting it in the ESP8266?

      In either cases, you should be able to do pretty much what you can with the HTTP client library with the WIFi Client library.

      HTTP works on top of sockets, and the WiFiClient basically is a socket.

      Nonetheless, it means that you need to implement everything from the HTTP protocol yourself.

      Unfortunately I don’t have code on how to use HTTP directly on top of the WiFIClient, but the Arduino core has a very simple example:
      https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WiFiClient/WiFiClient.ino

      Nonetheless, I really don’t recommend to implement HTTP on top of the WiFiClient yourself, unless you have a really good reason to do it and a good understanding about how the protocol works.

      Sending a request to a server and returning the response is something that can be achieved with a couple of lines using the HTTP Client library.

      It may seem too much work to change your code to use the HTTP Client, but in the long term it will most likely be easier to maintain and change.

      Hope this helps getting you in the right path πŸ™‚

      Best regards,
      Nuno Santos

  26. Hi there, thanks for the tutorial! Do you know if there’s a way of doing this with the Wifi Client library? Basically I am running a local server with a page connected to a remote server. The remote server returns a json file. However I am not able to retrieve in the code. I’ll post here just my void loop() since it’s a bit long (my full code is here – https://forum.arduino.cc/index.php?topic=559170.new#new – and I’d be really greatful if you could answer here since I’m not getting much help over there). Thanks!!
    void loop(){
    WiFiClient client = server.available();
    if (!client) {
    return;
    }
    // Wait until the client sends some data
    SPL(“new client”);
    while(!client.available()){
    delay(1);
    }
    //this doesn’t print the payload (which sould be the json. It works well in the browser)
    byte b;
    while (client.readBytes(&b, 1) > 0) {
    Serial.write(b);*
    }
    client.flush();
    // Prepare the response
    String s = “HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n”;
    s += page; //the html page hosted on the local server and which connects to the remote one
    s += “\n”;
    // Send the response to the client
    client.print(s);
    }

    1. Hi!
      You’re welcome πŸ™‚
      Well that’s a complex architecture, may I ask why can’t you directly fetch the json from the remote server?
      When you mention that you are hosting a server locally, are you hosting it on a computer on the same network of the ESP8266, or are you actually hosting it in the ESP8266?
      In either cases, you should be able to do pretty much what you can with the HTTP client library with the WIFi Client library.
      HTTP works on top of sockets, and the WiFiClient basically is a socket.
      Nonetheless, it means that you need to implement everything from the HTTP protocol yourself.
      Unfortunately I don’t have code on how to use HTTP directly on top of the WiFIClient, but the Arduino core has a very simple example:
      https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WiFiClient/WiFiClient.ino
      Nonetheless, I really don’t recommend to implement HTTP on top of the WiFiClient yourself, unless you have a really good reason to do it and a good understanding about how the protocol works.
      Sending a request to a server and returning the response is something that can be achieved with a couple of lines using the HTTP Client library.
      It may seem too much work to change your code to use the HTTP Client, but in the long term it will most likely be easier to maintain and change.
      Hope this helps getting you in the right path πŸ™‚
      Best regards,
      Nuno Santos

  27. Hi there, I only receive -1 as httpCode, no matter which website I try (even http://www.google.com). How can it be? I’m running this inside the setup since I only need it once. Thanks!

    HTTPClient http;
    http.begin(“http://www.google.com”);
    int httpCode = http.GET();

    Serial.print(“HTTP CODE: “);
    Serial.println(httpCode); //prints out HTTP CODE: -1

    if(httpCode > 0){ //doesn’t print and payload is empty
    String payload = http.getString();
    Serial.print(“PAYLOAD: “);
    Serial.println(payload);
    }

    http.end();

    1. Hi!

      Did you previously connect the device to a WiFi network and waited for the connection to complete?

      If you test the code exactly like it is on this post, does it also return -1 in every call?

      Let me know some more details so I can try to help πŸ™‚

      Best regards,
      Nuno Santos

  28. Hi there, I only receive -1 as httpCode, no matter which website I try (even http://www.google.com). How can it be? I’m running this inside the setup since I only need it once. Thanks!
    HTTPClient http;
    http.begin(“http://www.google.com”);
    int httpCode = http.GET();
    Serial.print(“HTTP CODE: “);
    Serial.println(httpCode); //prints out HTTP CODE: -1
    if(httpCode > 0){ //doesn’t print and payload is empty
    String payload = http.getString();
    Serial.print(“PAYLOAD: “);
    Serial.println(payload);
    }
    http.end();

    1. Hi!
      Did you previously connect the device to a WiFi network and waited for the connection to complete?
      If you test the code exactly like it is on this post, does it also return -1 in every call?
      Let me know some more details so I can try to help πŸ™‚
      Best regards,
      Nuno Santos

  29. Hi, I’m using the script to download a binary firmware file, which I then need to pass to the upload library as ‘Stream &data’. I can check the headers and content-type using http.collecteHeaders, but can’t workout how to pass the received file into the stream – using .getString() returns an empty string.

    1. Hi!

      I haven’t yet worked with streams and HTTP on the ESP8266, so I cannot help much.

      My suggestion is to ask around the ESP8266 Arduino core, so maybe someone there has already done it and can help πŸ™‚

      Best regards,
      Nuno Santos

  30. Hi, I’m using the script to download a binary firmware file, which I then need to pass to the upload library as ‘Stream &data’. I can check the headers and content-type using http.collecteHeaders, but can’t workout how to pass the received file into the stream – using .getString() returns an empty string.

    1. Hi!
      I haven’t yet worked with streams and HTTP on the ESP8266, so I cannot help much.
      My suggestion is to ask around the ESP8266 Arduino core, so maybe someone there has already done it and can help πŸ™‚
      Best regards,
      Nuno Santos

  31. Hey,

    Im trying to get your sketch uploaded to the ESP-01 which is connected to my Arduino. I have done the connections properly(http://www.teomaragakis.com/wp-content/uploads/2015/10/uno_esp_connection.png) and also the sketch compiles successfully. The issue occurs when I try to upload the sketch. The following errors are displayed:

    warning: espcomm_sync failed
    error: espcomm_open failed
    error: espcomm_upload_mem failed
    error: espcomm_upload_mem failed

    Please do check the link and let me know if the connections are right.
    Would be great if you can help me out!Thanks cheers!

    1. Hi,

      Were you able to upload any other sketches in the past with the same hardware configuration or is it the first time that you try?

      Unfortunately I never tried to upload code to the ESP8266 using an Arduino, so I cannot help much.

      Nonetheless, here is the hardware configuration needed to directly upload code to the ESP-01, without the need of using Arduino. Note that this post already has some time, but I think it is still working πŸ™‚
      https://techtutorialsx.com/2016/02/28/esp8266-uploading-code-from-arduino-ide/

      Best regards,
      Nuno Santos

  32. Hey,
    Im trying to get your sketch uploaded to the ESP-01 which is connected to my Arduino. I have done the connections properly(http://www.teomaragakis.com/wp-content/uploads/2015/10/uno_esp_connection.png) and also the sketch compiles successfully. The issue occurs when I try to upload the sketch. The following errors are displayed:
    warning: espcomm_sync failed
    error: espcomm_open failed
    error: espcomm_upload_mem failed
    error: espcomm_upload_mem failed
    Please do check the link and let me know if the connections are right.
    Would be great if you can help me out!Thanks cheers!

    1. Hi,
      Were you able to upload any other sketches in the past with the same hardware configuration or is it the first time that you try?
      Unfortunately I never tried to upload code to the ESP8266 using an Arduino, so I cannot help much.
      Nonetheless, here is the hardware configuration needed to directly upload code to the ESP-01, without the need of using Arduino. Note that this post already has some time, but I think it is still working πŸ™‚
      https://techtutorialsx.com/2016/02/28/esp8266-uploading-code-from-arduino-ide/
      Best regards,
      Nuno Santos

Leave a Reply