requests2 — Network Request Module

requests2 is based on urequests and supports Streaming Uploads and x-www-form-urlencoded.

The main functionality and function of the requests2 module.

Micropython Example:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8import requests2
 9
10
11label0 = None
12http_req = None
13
14
15def setup():
16    global label0, http_req
17
18    M5.begin()
19    Widgets.fillScreen(0x222222)
20    label0 = Widgets.Label("label0", 6, 6, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
21
22    http_req = requests2.get(
23        "https://httpbin.org/get", headers={"Content-Type": "application/json"}
24    )
25    label0.setText(str(http_req.text))
26
27
28def loop():
29    global label0, http_req
30    M5.update()
31
32
33if __name__ == "__main__":
34    try:
35        setup()
36        while True:
37            loop()
38    except (Exception, KeyboardInterrupt) as e:
39        try:
40            from utility import print_error_msg
41
42            print_error_msg(e)
43        except ImportError:
44            print("please update to latest firmware")

UIFLOW2 Example:

example.png

cores3_http_get_example.m5f2

Function

requests2.request(method, url, data=None, json=None, headers={}) Response

Send a network request, it will block the response data returned to the network, parameters:

Parameters:
  • method (str) – method of establishing a network request. e.g. HEAD,``GET``,``POST``,``PUT``,``PATCH``, DELETE.

  • url (str) – URL of the network request.

  • data – (optional), a dictionary, tuple list [(key, value)] (will be form coded), byte or class file object sent in the request body.

  • json – (optional), json data sent in the request body.

  • headers (dict) – (optional), HTTP header dictionary to be sent with the request.

requests2.head(url, **kw) Response

Send a HEAD request, the return type is the response of the request, parameters:

Parameters:
  • url (str) – URL of the network request.

  • kw – request optional parameters.

requests2.get(url, **kw) Response

Send a GET request, the return type is the response of the request, parameters:

Parameters:
  • url (str) – URL of the network request.

  • kw – request optional parameters.

UIFLOW2:

get.png

requests2.post(url, **kw) Response

Send a POST request, the return type is the response of the request, parameters:

Parameters:
  • url (str) – URL of the network request.

  • kw – request optional parameters.

UIFLOW2:

post.png

requests2.put(url, **kw) Response

Send a PUT request, the return type is the response of the request, parameters:

Parameters:
  • url (str) – URL of the network request.

  • kw – request optional parameters.

UIFLOW2:

put.png

requests2.patch(url, **kw) Response

Send a PATCH request, the return type is the response of the request, parameters:

Parameters:
  • url (str) – URL of the network request.

  • kw – request optional parameters.

UIFLOW2:

patch.png

requests2.delete(url, **kw) Response

Send a DELETE request, the return type is the response of the request, parameters:

Parameters:
  • url (str) – URL of the network request.

  • kw – request optional parameters.

UIFLOW2:

delete.png

class Response

Methods

Response.headers
Type:

dict

Return the response header.

UIFLOW2:

headers.png

Response.status_code
Type:

int

Return the status code of the response.

UIFLOW2:

status_code.png

Response.close() None

Close the connection and release resources.

UIFLOW2:

close.png

property Response.content
Type:

dict

Return the content of the response, in bytes.

UIFLOW2:

content.png

property Response.text
Type:

str

Return the content of the response, in str.

UIFLOW2:

text.png

Response.json() dict

Return the content of the response, in dict.

UIFLOW2:

json.png