python requests post form data x-www-form-urlencoded

Using Python Requests to Post Form Data x-www-form-urlencoded

If you want to send a POST request with form data in x-www-form-urlencoded format using Python Requests, you can do so using the data parameter.

Example: POST request with form data


import requests

url = 'https://example.com/submit-form'
data = {'name': 'John', 'age': 30}

response = requests.post(url, data=data)

print(response.content)
    

In the example above, we are sending a POST request to 'https://example.com/submit-form' with form data consisting of a name and age field. The response is then printed to the console.

You can also send form data as a string using the data parameter, like so:

Example: POST request with form data as a string


import requests

url = 'https://example.com/submit-form'
data = 'name=John&age=30'

response = requests.post(url, data=data)

print(response.content)
    

In this example, we are sending the same form data as a string instead of a dictionary.

It's important to note that when sending form data in x-www-form-urlencoded format, the data should be URL encoded. However, Python Requests will automatically handle this encoding for you when using the data parameter.

Overall, sending POST requests with form data in x-www-form-urlencoded format is easy using Python Requests. Just use the data parameter with either a dictionary or a string.