Sending Form Data with Python Requests

Oct 22, 2023 ยท 4 min read

Sending form data is a common task in web development. Whether you are building a web app, scraping a site, or interacting with APIs, you'll need to submit forms and encode data in your Python code. In this comprehensive guide, we'll cover all the key aspects of sending form data with the fantastic Requests library.

An Introduction to Sending Form Data with Requests

The Python Requests module provides an elegant and simple HTTP library for your Python projects. With Requests, you can submit forms, upload files, and send JSON data seamlessly.

The key reasons to use Requests for sending form data are:

  • Simple and intuitive API
  • Built-in form encoding and multipart support
  • Automatic JSON serialization
  • Persistent sessions with cookies
  • Connection pooling and timeouts
  • Works with Python 2 and 3
  • To get started, install Requests with pip install requests and import it:

    import requests
    

    Now let's look at how to send different types of form data.

    Submitting Application/x-www-form-urlencoded Form Data

    This is the default encoding used by HTML forms. To send this type of form data:

    1. Add your form data to a dictionary:
    2. Make a POST request and pass the data in the data parameter:

    This will encode the data dictionary and set the Content-Type header automatically.

    You can also manually encode the data using urllib or urllib.parse.urlencode and set the Content-Type header yourself for more control.

    Uploading Files and Multipart Form Data

    To upload files and attachments, you need to send a multipart form request. With Requests, this is straightforward:

    1. Create files dictionary mapping file names to file objects:
    2. Make a POST request, passing files instead of data:

    You can also attach text fields and custom headers by using tuple values with up to four items:

    files = {
      'file1': ('report.pdf', open('report.pdf','rb'), 'application/pdf'),
      'field': (None, 'value')
    }
    

    The Requests module handles all the complexity of multipart encoding behind the scenes.

    Sending JSON Data with a POST Request

    JSON is a popular format for passing structured data. To send JSON, pass your Python dict to the json parameter:

    data = {
      'name': 'John Doe',
      'hobbies': ['hiking', 'diving']
    }
    
    response = requests.post(url, json=data)
    

    The data will be JSON-encoded, and the correct Content-Type header will be set.

    Alternatively, you can manually serialize the dict to JSON using json.dumps() and set the header yourself.

    Additional Tips and Tricks

    Here are some additional tips when posting forms with Python Requests:

  • Check the status code of the Response to catch errors
  • Use a Session for efficiency and persistent cookies
  • Set timeouts to avoid blocking on long requests
  • Handle SSL verification and certificates for HTTPS sites
  • Enable logging to debug issues
  • Use encoding, user-agents, and other headers to mimic browsers
  • Compress data with Gzip when sending large requests
  • Stream multipart uploads from disk to handle large files
  • And those are the key things you need to know to master sending form data seamlessly using Python Requests!

    FAQs about Sending Form Data with Python Requests

    How do you pass form data in Python requests?

    Pass form data to the data parameter of requests.post() and requests.put() as a dictionary. Requests will URL-encode and set the Content-Type header automatically.

    How can I store HTML form data in Python?

    When you receive a form submission in Python (e.g. in Flask), access it through request.form which contains a MultiDict of the form data. Store individual values in variables or insert them into a database.

    What is the best way to send JSON data with Python Requests?

    Pass your JSON-serializable dict to the json argument of a post() or put() request. Requests will serialize the data to JSON and set the Content-Type for you.

    How do I upload multipart form data and files?

    Use the files argument instead of data when making a request. The value should be a dict mapping filenames to file objects. Requests handles all the multipart encoding for you.

    How can I debug issues with sending form data?

    Enable Requests logging to help debug errors. Additionally, check response status codes and inspect request headers to ensure form data is encoded properly.

    Hopefully this guide provides a comprehensive overview of how to effectively send JSON, multipart, and form-url encoded data with Python Requests!

    Browse by tags:

    Browse by language:

    The easiest way to do Web Scraping

    Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


    Try ProxiesAPI for free

    curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

    <!doctype html>
    <html>
    <head>
        <title>Example Domain</title>
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    ...

    X

    Don't leave just yet!

    Enter your email below to claim your free API key: