mrequests - HTTP client module for MicroPython
HTTP client module for MicroPython
0
Project description
below description is copied from Github readme file. Link is given above.
mrequests
An HTTP client module for MicroPython with an API similar to requests.
This is an evolution of the urequests module from micropython-lib with a few extensions and many fixes and convenience features.
Features & Limitations
Compatibility
Supports the unix, stm32, esp8266 and esp32 MicroPython ports as well as CPython.
On the unix, stm32 and esp8266 ports the SSL/TLS support has some limitations due to problems with MicroPython's ssl module on these platforms.
On the stm32 port installing a custom-compiled firmware with network/SSL support is required.
Features
- Supports redirection with absolute and relative URLs (see below for details).
- Supports HTTP basic authentication (requires
ubinasciimodule). - Supports socket timeouts.
- Response headers can optionally be saved in the response object.
- Respects
Content-lengthheader in response. - Supports responses with chunked transfer encoding.
Responseobjects have asavemethod to save the response body to a file, reading the response data and writing the file in small chunks.- The
Responseclass for response objects can be substituted by a custom response class.
Limitations
mrequests.requestis a synchroneous, blocking function.- The code is not interrupt save and a fair amount of memory allocation is happening in the process of handling a request.
- URL parsing does not cover all corner cases (see test_urlparse for details).
- URLs with authentication credentials in the host part (e.g.
http://user:secret@myhost/) are not supported. Pass authentication credentials separately via theauthargument instead. - SSL/TLS support on the MicroPython unix, stm32 and esp8266 ports is limited. In particular, their
sslmodule does not support all encryption schemes commonly in use by popular servers, meaning that trying to connect to them via HTTPS will fail with various cryptic error messages. - Request and JSON data may be passed in as bytes or strings and the request data will be encoded to bytes, if necessary, using the encoding given with the
encodingparameter. But be aware that encodings other thanutf-8are not supported by most (any?) MicroPython implementations. - Custom headers may be passed as a dictionary with string or bytes keys and values and must contain only ASCII chars. If you need header values to use non-ASCII chars, you need to encode them according to RFC 8187.
- The URL and specifically any query string parameters it contains will not be URL-encoded, and it may contain only ASCII chars. Make sure you encode the query string part of the URL with
urlencode.quotebefore passing it, if necessary. - When encoding
strinstances viaurlencode.urlencodeorurlencode.quote, theencodinganderrorsarguments are currently ignored by MicroPython and it behaves as if their values were"utf-8"resp."ignore". - In responses using "chunked" transfer-encoding, chunk extensions and trailers are ignored.
Redirection Support
- Can follow redirects for response status codes 301, 302, 303, 307 and 308.
- The HTTP method is changed to
GETfor redirects, unless the original method wasHEADor the status code is 307 or 308. - For status code 303, if the method of the request resulting in a redirection (which may have been the result of a previous redirection) is
GET, the redirection is not followed, since theLocationheader is supposed to indicate a non-HTTP resource then. - Redirects are allowed to change the protocol from
httptohttps, but redirects changing fromhttpstohttpwill not be followed. - The
requestfunction has an additional keyword argumentmax_redirects, defaulting to 1, which controls how many level of redirections are followed. If this is exceeded, the function raises aValueError. - The code does not check for infinite redirection cycles. It is advised to keep
max_redirectsto a low number instead.
