ESP32 / ESP8266 MicroPython: HTTP GET Requests

The objective of this post is to explain how to perform HTTP GET requests with MicroPython, using the urequests module. This tutorial was tested with MicroPython running on both the ESP32 and the ESP8266.


Introduction

The objective of this post is to explain how to perform HTTP GET requests with MicroPython, using the urequests module. This tutorial was tested with MicroPython running on both the ESP32 and the ESP8266. The figures shown bellow were taken from the tests on the ESP32, but the results on the ESP8266 are similar. You can access the source code of the library here.

Important: At the time of writing, the version of MicroPython used had urequests included. Note this can change and future distributions may not include it by default. So, if you can’t simply import it, you may have to install it manually before.

Naturally, to follow this tutorial, we need to connect to a WiFi network first, so we have Internet access. I will not cover how to do it in this tutorial. If you want to connect manually, please check this detailed guide. If you wan’t to implement an automatic connection to a WiFi network when MicroPython boots, please check this other guide.

We will run the code by connecting to the MicroPython prompt and sending a command at a time. If you prefer, you can write the commands in a script and run the script from your computer, as explained here. Another option is to upload the script to MicroPython’s file system and run it from the prompt, as can be seen here.

I will be using Putty to connect to the prompt, but you may use other software that allows to establish a serial connection. Please consult this previous post if you haven’t yet configured MicroPython on your ESP32. If you are using the ESP8266, check this one.

 

The code

First of all and after being connected to a WiFi network, we will import the urequests module, which has all the functions we need for this tutorial.

import urequests

Next, to perform the actual HTTP GET request, we simply need to call the get function of the urequests module, passing as input the URL of destination of the request. We will use a website with a fake online REST API, which I’ve used before in many tutorials.

The URL of the website that we are going to reach with a HTTP GET request is http://jsonplaceholder.typicode.com/albums/1 and you can confirm the expect result by accessing it on a web browser. Upon accessing it, you should get something similar to figure 1, which is a JSON structure of a dummy album object.

Online Fake REST testing API

Figure 1 – Expected output of a HTTP GET request.

The code to perform the mentioned GET request from MicroPython is shown bellow. As output of the call to the get function, an object of class Response will be returned.

response = urequests.get('http://jsonplaceholder.typicode.com/albums/1')
print(type(response))

As you can confirm in figure 2, the returned object is indeed of class Response.

ESP32 ESP8266 MicroPython urequests GET Method

Figure 2 – Class of the object returned by the call to the get method.

Now, to access the actual content of the response of the HTTP request, we just need to access its text property, as indicated bellow.

 
print(response.text)
print(type(response.text))

You can check bellow at figure 3 the expected output of this command.

ESP32 ESP8266 MicroPython response as string

Figure 3 – Printing the response of the HTTP GET request.

Note that this property returns a string with the content. Nevertheless, as we have seen, the response of the request is returned in JSON format. So, if we prefer, we can access the json property, which returns a dictionary containing the parsed content.

parsed = response.json()
print(type(parsed))

The result of these commands is shown in figure 4. It confirms that our result is indeed a Python dictionary.

ESP32 ESP8266 MicroPython HTTP GET parsing JSON

Figure 4 – Obtaining the JSON content of the response parsed.

As can be seen in the source code of the library, this property uses the ujson library to parse the content. You can learn more about ujson parsing in this previous post. Now that we have the parsed object, we can access each individual JSON value by using its key on our dictionary object.

 
print(parsed["userId"])
print(parsed["id"])
print(parsed["title"])

Check bellow at figure 5 the result of accessing each of the values of the JSON structure. The result is coherent with the JSON structure obtained from the text property.

ESP8266 ESP32 MicroPython HTTP GET JSON dictionary

Figure 5 – Accessing the values of the JSON structure via Python dictionary.

Other interesting way of retrieving the content of the response of the request is using the content property, which will return the response in bytes.

 
print(response.content)
print(type(response.content))

Check bellow in figure 6 the result of the commands.

ESP32 ESP8266 MicroPython HTTP GET Answer Bytes

Figure 6 – Response of the HTTP GET request as bytes.

To finalize, we will also get the status code and the textual reason of that status code. To do so, we use the status_code and reason attributes, as can be seen in the code bellow.

print(response.status_code)
print(response.reason)

Figure 7 illustrates the result of these commands. Note that the result has a code 200, which corresponds to a “OK” code in HTTP.

ESP32 ESP8266 MicroPython urequests status code

Figure 7 – Getting the HTTP status code and reason.


Related content


Related posts

8 thoughts on “ESP32 / ESP8266 MicroPython: HTTP GET Requests”

  1. Pingback: ESP32 / ESP8266 MicroPython: HTTP POST Requests | techtutorialsx

  2. Pingback: ESP32 / ESP8266 MicroPython: HTTP POST Requests | techtutorialsx

  3. The urequests library has a hard coded HTTP 1.0 that causes response error 505 with my web servers. Woud be great if HTTP version was configurable.

    In my case I solved it by making a custom ‘urequests1’ library uploaded to the device and imported to main application instead of urequests.

    1. Hi!

      I haven’t yet run into issues with that, but thanks for sharing the information, I was not aware of the fact that it was hardcoded there.

      It would indeed be very useful to have that configurable, maybe bringing this issue to the attention on the library maintainers at GitHub would make them consider that?

      Best regards,
      Nuno Santos

  4. The urequests library has a hard coded HTTP 1.0 that causes response error 505 with my web servers. Woud be great if HTTP version was configurable.
    In my case I solved it by making a custom ‘urequests1’ library uploaded to the device and imported to main application instead of urequests.

    1. Hi!
      I haven’t yet run into issues with that, but thanks for sharing the information, I was not aware of the fact that it was hardcoded there.
      It would indeed be very useful to have that configurable, maybe bringing this issue to the attention on the library maintainers at GitHub would make them consider that?
      Best regards,
      Nuno Santos

  5. hello, I follow exactly your tutorial.
    however when I run the response = urequests.get(‘http://jsonplaceholder.typicode.com/albums/1’)
    I got this error :
    File “main.py”,line 3, in
    File “/flash/lib/urequests.py”,line 112, in get
    File “/flash/lib/urequests.py”,line 53, in request
    Type Error : function takes 2 proportional arguements but 4 were given.

    Can i know how to solve this? thanks

    1. Hi!

      Unfortunately I cannot be of much assistance since I never ran into that issue and I’ve not been playing with micro python for a while.

      My guess is that something has changed in the library since the tutorial has been published. My recommendation is that you open an issue on the github page of MicroPython.
      https://github.com/micropython/micropython-lib/tree/master/urequests

      I’ve been taking a look at the library source tough and it seems that the get method call still has the same signature, so it really may be some bug.
      https://github.com/micropython/micropython-lib/blob/master/urequests/urequests.py#L111

      Let us know if you succeed on solving the issue.

      Best regards,
      Nuno Santos

      1. yes, i did. need to change urequest.py line 53 to this
        ai = usocket.getaddrinfo(host, port)

        but my microcontroller freeze if i put in while loop.
        it can only get one time.

        1. Do you have any delay between requests?

          maybe it is an issue related with the frequency of the requests. I’m not sure how micropython handles the requests under the hood.

          You can post the code you are using here in a comment, maybe someone someone spots the issue 🙂

          Best regards,
          Nuno Santos

  6. hello, I follow exactly your tutorial.
    however when I run the response = urequests.get(‘http://jsonplaceholder.typicode.com/albums/1’)
    I got this error :
    File “main.py”,line 3, in
    File “/flash/lib/urequests.py”,line 112, in get
    File “/flash/lib/urequests.py”,line 53, in request
    Type Error : function takes 2 proportional arguements but 4 were given.
    Can i know how to solve this? thanks

    1. Hi!
      Unfortunately I cannot be of much assistance since I never ran into that issue and I’ve not been playing with micro python for a while.
      My guess is that something has changed in the library since the tutorial has been published. My recommendation is that you open an issue on the github page of MicroPython.
      https://github.com/micropython/micropython-lib/tree/master/urequests
      I’ve been taking a look at the library source tough and it seems that the get method call still has the same signature, so it really may be some bug.
      https://github.com/micropython/micropython-lib/blob/master/urequests/urequests.py#L111
      Let us know if you succeed on solving the issue.
      Best regards,
      Nuno Santos

      1. yes, i did. need to change urequest.py line 53 to this
        ai = usocket.getaddrinfo(host, port)
        but my microcontroller freeze if i put in while loop.
        it can only get one time.

        1. Do you have any delay between requests?
          maybe it is an issue related with the frequency of the requests. I’m not sure how micropython handles the requests under the hood.
          You can post the code you are using here in a comment, maybe someone someone spots the issue 🙂
          Best regards,
          Nuno Santos

  7. hello, I follow exactly your tutorial.
    however when I run the response = urequests.get(‘http://jsonplaceholder.typicode.com/albums/1’)
    I got this error :
    File “main.py”,line 3, in
    File “/flash/lib/urequests.py”,line 112, in get
    File “/flash/lib/urequests.py”,line 53, in request
    Type Error : function takes 2 proportional arguements but 4 were given.

    Can i know how to solve this? thanks

  8. hello, I follow exactly your tutorial.
    however when I run the response = urequests.get(‘http://jsonplaceholder.typicode.com/albums/1’)
    I got this error :
    File “main.py”,line 3, in
    File “/flash/lib/urequests.py”,line 112, in get
    File “/flash/lib/urequests.py”,line 53, in request
    Type Error : function takes 2 proportional arguements but 4 were given.
    Can i know how to solve this? thanks

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading