An example of posting an HTML form using the Python Requests library.
In this post HTML form example, the import requests directive imports the Requests library to our python code. The headers["Content-Type"] = "application/x-www-form-urlencoded" line sets the MIME type to application/x-www-form-urlencoded for the data in the body of POST message and requests.post(url, headers=headers, data=data) line sends data to the server.
Posting JSON data with the Requests library is also very easy. To do this, you need to set Content-Type to application/json and pass JSON to requests.post() method.
In this post JSON example, we also send the Accept: application/json request header to tell the server that our client is expecting JSON. Without this header, the server may return data in a different format.
Uploading files using Python Requests Library is also very easy.
To upload multiple files, simply pass the list of files to the requests.post() method.
Getting Started with Python Requests LibraryTo use the Python Requests Library, you need to install it first. You can do this with the following command:
If you prefer using Pipenv, you can run the following command:
After installing the Requests library, you can use it in your code by importing the requests module with the following code:
For more information, visit the Python Requests Library home page.