urequests — HTTP client functions

Previously we used the socket library, which is a good tool for getting started and helps you understand some basic concepts of web crawlers and master the crawling process. After getting started, we need to learn some more advanced content and tools to make crawling easier. So this section briefly introduces the basic usage of the urequests library.

Response Class

class urequests.Response(s)

The object of the Response class, which contains the server’s response to an HTTP request.

  • s - The ussl object

Methods

urequests.close()

Close the socket.

@urequests.content

Return the content of the response, in bytes.

@urequests.text

Return the content of the response as text, encoded as unicode.

urequests.json()

Return the json-encoded content of the response and convert it to a dict type.

Methods

urequests.request(method, url, data=None, json=None, headers={}, params=None, files=None)

Send an HTTP request to the server.

  • method - The HTTP method to use

  • url - The URL to send to

  • data - The body to attach to the request. If a dictionary or a list of tuples is provided, form encoding will be performed.

  • json - The json to attach to the body of the request.

  • headers - The dictionary of headers to send.

  • params - The URL parameters to append to the URL. If a dictionary or a list of tuples is provided, form encoding will be performed.

  • files - Used for file upload, of type 2-tuple, which defines the file name, file path, and content type. As follows, {‘name’, (file directory, content-type)}

urequests.head(url, **kw)

Send a HEAD request and return a Response object.

  • url - The URL of the Request object

  • **kw - The parameters of the request method.

urequests.get(url, **kw)

Send a GET request and return a Response object.

  • url - The URL of the Request object

  • **kw - The parameters of the request method.

urequests.post(url, **kw)

Send a POST request and return a Response object.

  • url - The URL of the Request object

  • **kw - The parameters of the request method.

urequests.put(url, **kw)

Send a PUT request and return a Response object.

  • url - The URL of the Request object

  • **kw - The parameters of the request method.

urequests.patch(url, **kw)

Send a PATCH request and return a Response object.

  • url - The URL of the Request object

  • **kw - The parameters of the request method.

urequests.delete(url, **kw)

Send a DELETE request and return a Response object.

  • url - The URL of the Request object

  • **kw - The parameters of the request method.

requests example
 1import urequests
 2from mpython import *
 3
 4# Connect to the network
 5my_wifi = wifi()
 6my_wifi.connectWiFi('', '')
 7
 8# Visit the ip address api
 9r = requests.get("http://ip-api.com/json/")
10print(r)
11print(r.content)  # Return the response content
12print(r.text)  # Return the response content as text
13print(r.content)
14print(r.json())  # Return the json-encoded response content and convert it to a dict type
15
16# It's mandatory to close response objects as soon as you finished
17# working with them. On MicroPython platforms without full-fledged
18# OS, not doing so may lead to resource leaks and malfunction.
19r.close()