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:
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:
- 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:
- 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: