Skip to content

mrequests - HTTP client module for MicroPython

HTTP client module for MicroPython

viktor

Published September 20, 2023

Original author: Christopher ArndtOriginal source (new tab)

mrequests -  HTTP client module for MicroPython

Components

Hardware components

WIZnet parts

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 ubinascii module).
  • Supports socket timeouts.
  • Response headers can optionally be saved in the response object.
  • Respects Content-length header in response.
  • Supports responses with chunked transfer encoding.
  • Response objects have a save method to save the response body to a file, reading the response data and writing the file in small chunks.
  • The Response class for response objects can be substituted by a custom response class.

Limitations

  • mrequests.request is 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 the auth argument instead.
  • SSL/TLS support on the MicroPython unix, stm32 and esp8266 ports is limited. In particular, their ssl module 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 encoding parameter. But be aware that encodings other than utf-8 are 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.quote before passing it, if necessary.
  • When encoding str instances via urlencode.urlencode or urlencode.quote, the encoding and errors arguments 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 GET for redirects, unless the original method was HEAD or 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 the Location header is supposed to indicate a non-HTTP resource then.
  • Redirects are allowed to change the protocol from http to https, but redirects changing from https to http will not be followed.
  • The request function has an additional keyword argument max_redirects, defaulting to 1, which controls how many level of redirections are followed. If this is exceeded, the function raises a ValueError.
  • The code does not check for infinite redirection cycles. It is advised to keep max_redirects to a low number instead.

Comments

Similar projects you might like

Comments