- Send Form Data With Requests in Python
- Example 1
- Example 2
- Example 3
- Python requests: POST Request Explained
- Understanding the Python requests POST Function
- Send a HTTP POST request with Python requests library
- Send a simple POST in Python requests
- Send POST request with complex HTML form-encoded data
- Send POST request with JSON data as string
- Send Multipart-Encoded POST request
- Send multiple files in a Multipart-Encoded POST request
- Send Large Multipart-Encoded POST request
Send Form Data With Requests in Python
The requests library is a great tool for making requests to the web using Python. This article will discuss using this module to send form data to a site. In particular, we will be using the requests.post(url, data=None, json=None) function.
For demonstration purposes, we will send POST requests to https://httpbin.org – an open-source site that allows one to inspect HTTPS requests before using them in production.
Example 1
Let’s start by posting a string to httpbin.org and see the result.
Output (truncated):
< "args": <>, "data": "value1", "files": <>, "form": <>, … "json": null, "origin": "105.161.11.xxx", "url": "https://httpbin.org/post" >
The server response in the output shows the data value we passed into requests.post() is posted under “data”. Form data should be posted on the “form” property. It is empty at the moment. Let’s post some form data in the following example.
Example 2
To pass form-encoded data, just like you will do on HTML forms, you need to pass dictionary-formatted items to the data argument on requests.posts().
The dictionary will be automatically passed as form data when the POST request is made. Here is an example.
Output (truncated):
< "args": <>, "data": "", "files": <>, "form": < "comments": "this is a comment", "custemail": "[email protected]", "custname": "Karl", "custtel": "01883", "delivery": "12:00", "size": "large", "topping": "cheese" >, … "origin": "105.161.11.xxx", "url": "https://httpbin.org/post" >
As shown in the output, the dictionary data was passed as a form when the request was made. That is why, in this example, the “form” property of the response has the data we passed to requests.post() function. Let’s go into another interesting example.
Example 3
The requests.post() also allows the user to pass multiple values to a single dictionary key (form property). This can be done in two ways:
- Using a list of tuples. Here is an example with two values for the “items” property.
data = [ ( «customer_name» , «Smith» ) , ( «delivery» , «12:00» ) , ( «items» , «item1» ) , ( «items» , «item2» ) ]
- Using a dictionary with all possible values of a given key in a list. The example in the first method can be represented as:
Python requests: POST Request Explained
In this tutorial, you’ll learn how to use the Python requests library’s POST function to post data via HTTP. The Python requests library abstracts the complexities in making HTTP requests. The requests.post() function allows you to post data to a web resource.
By the end of this tutorial, you’ll have learned:
- How the Python requests post function works
- How to customize the Python requests post function with headers
- How to use the Python response objects when working with the post function
Understanding the Python requests POST Function
An HTTP POST request is used to send data to a server, where data are shared via the body of a request. In the request.post() function, data are sent with the data parameter, which accepts a dictionary, a list of tuples, bytes or a file object.
Let’s take a look at what the requests.post() function looks like in Python:
# Understanding the requests.post() Function import requests requests.post( url, data=None, json=None, **kwargs )
We can see that the function accepts three parameters:
- url , which defines the URL to post to
- data , which accepts a dictionary, list of tuples, butes, or a file-like object to send in the body of the request
- json , which represents json data to send in the body of the request
Additionally, the function accept a number of different keyword arguments inherited from the requests.request() function. These keyword arguments can be passed in directly and are described in the table below:
Send a HTTP POST request with Python requests library
requests is an popular, simple yet powerful HTTP library for Python. It allows you to easily prepare and send complex HTTP requests in a few lines of code. Using requests library is a popular way to abstract away the complexity of managing query strings, arguments, form-encode POST data, and many other things when it comes to making HTTP requests.
In this article, we will show you a few ways to send a HTTP POST request in Python requests library, from the simple one to the more complex requests including multipart POST data.
Send a simple POST in Python requests
A POST request is typically sent via an HTML form and results in a change on the server. HTTP POST method is often used when submitting login or contact forms or uploading files and images to the server.
In order to send a simple HTTP POST request with some form-encoded data, use the post method with data argument.
response = requests.post('https://httpbin.org/post', data=post_data) print(response) # OUTPUT
In the code snippet above, post_data is a Python dictionary contains all the POST data that you want to send out.
Send POST request with complex HTML form-encoded data
If you want to emulate a request sent by a HTML form, which contains form-encoded data, you can pass the data argument just like the example above. The data argument can also take in a tuple-based payload or a dictionary-based one with multiple elements in the same key.
r2 = requests.post('https://httpbin.org/post', data=payload_dict) print(r1.text == r2.text) # OUTPUT True
In the example above, the two tuple-based and dictionary-based payload does the same thing, which is sending a form-encoded data that use the same key for multiple field.
Send POST request with JSON data as string
There are times that you may want to send a JSON as string to a server via a POST request. For example, a few API only accepts complex data to be able to simplify the request body. If you pass in a string instead of a dict , the JSON data will be posted directly to the URL. In this example, we manually encode the JSON data to string with Python json library.
response = requests.post(url, data=json.dumps(payload))
If you’re using a more recent version of requests (>2.4.2), you can pass a json argument directly instead of manually encoding the dict.
response = requests.post(url, json=payload)
Please note that the only difference between data and json argument is that the json will set the Content-Type in the header to application/json , while POST requests sent with data will have Content-Type: application/x-www-form-urlencoded header.
The json parameter would be ignored if either data or files is passed.
Send Multipart-Encoded POST request
A HTTP multipart request is a HTTP request that HTTP clients construct to send files and data over to a HTTP Server. It is commonly used by browsers and HTTP clients to upload files to the server. Those files can be photos, music, binary, text or a combination of them.
If you didn’t know how it would look like in raw form, here it is.
POST /test HTTP/1.1 Host: example User-Agent: Mozilla/5.0 Gecko/2009042316 Firefox/3.0.10 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: http://example/example.htm Content-Type: multipart/form-data; boundary=2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f Content-Length: 514 --2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f Content-Disposition: form-data; name="datafile1"; filename="a.gif" Content-Type: image/gif GIF87a. D..; --2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f Content-Disposition: form-data; name="datafile2"; filename="b.gif" Content-Type: image/gif GIF87a. D..; --2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f Content-Disposition: form-data; name="datafile3"; filename="c.gif" Content-Type: image/gif GIF87a. D..; --2a8ae6ad-f4ad-4d9a-a92c-6d217011fe0f--
As you can see, the multipart data is big, and contains many files inside as well as their filenames. In requests , multipart-encoded POST request can be easily made using files argument.
r = requests.post(url, files=files) r.text # OUTPUT < . "files": < "datafile1": "" >, . >
Please note that we opened the file in binary mode. I is strongly recommended that you open files in binary mode because requests attempt to automatically provide the Content-Length header for you. This value should be set to the number of bytes in the file, not the number of text characters in the string version of it. Errors may occur if you open the file in text mode.
If you need to send filenames, content types and other file details as well, use a 3-element tuple as value in the files dict. The tuple order should be (filename, file_object, content_type, expiration) .
) files = r = requests.post(url, files=files) r.text # OUTPUT < . "files": < "datafile1": "" >, . >
Send multiple files in a Multipart-Encoded POST request
You can send more than one file in a single POST with requests library as well. It’s a little different than when you send a single file. In this case, the files dictionary should be a list of 2-element tuples following (form_field_name, file_info) syntax, where file_info should be a 3-element tuple that contains file details.
'Content-Type': 'multipart/form-data; boundary=3131623adb2043caaeb5538cc7aa0b3a', . >
Files should be opened in binary mode, otherwise Content-Length header can be set to an incorrect value and causing errors.
Send Large Multipart-Encoded POST request
There are times that your files is relatively big, you may want to stream the request instead of reading all of its contents into memory.
By default, requests does not support this, but there is a separate package which does – requests-toolbelt .
This use case is pretty rare, but if you want to find out more about it, you should read the toolbelt’s documentation. There are a section dedicated to Uploading Data via HTTP POST request, which contains tutorials on Streaming large multipart data and monitoring that process.