Home > Backend Development > Python Tutorial > How to Send Multipart Form Data with Files and Standard Form Data in Python?

How to Send Multipart Form Data with Files and Standard Form Data in Python?

Patricia Arquette
Release: 2024-12-25 07:51:12
Original
539 people have browsed it

How to Send Multipart Form Data with Files and Standard Form Data in Python?

How to Construct Multipart Form Data Requests in Python

Question: How can I send multipart form data in Python using the requests library? While I understand how to attach files, I'm struggling to incorporate standard form data into such requests.

Answer:

Requests automatically handles multipart form data when a files parameter is specified, resulting in a multipart/form-data POST request instead of an application/x-www-form-urlencoded POST.

Syntax:

from requests import post

response = post(
    url,
    files={
        "form_field_name": "form_field_value",     # No quotes needed for non-string values
    }
)
Copy after login

Example:

response = post("http://httpbin.org/post", files={"foo": "bar"})
assert response.status_code == 200
Copy after login

Advanced Control:

Use tuples to customize the filename, content type, and additional headers for each part. Tuple components include:

  • Filename (optional)
  • Content
  • Content type (optional)
  • Header dictionary (optional)

Example:

files = {"foo": (None, "bar")}   # No filename specified
Copy after login

Ordered Multiple Fields:

Use a list of tuples for ordered or multiple fields with the same name.

Handling Data and Files:

When using both data and files, a string data parameter will take precedence. Otherwise, both data and files are combined in the request.

Optional Libraries:

The requests-toolbelt project provides advanced Multipart support, allowing for:

  • Streaming from file objects
  • No default filename parameters
  • Custom filename, part mime type, and extra headers using tuples

Example with requests-toolbelt:

import MultipartEncoder from requests_toolbelt

fields = {
    "foo": b"bar",    # Fields support bytes objects
    "spam": ("spam.txt", open("spam.txt", "rb"), "text/plain")   # Stream files
}

mp_encoder = MultipartEncoder(fields)

response = post(
    url,
    data=mp_encoder,
    headers={"Content-Type": mp_encoder.content_type}
)
Copy after login

Note: For the requests-toolbelt method, do not use files= argument as the MultipartEncoder is posted as the data payload.

The above is the detailed content of How to Send Multipart Form Data with Files and Standard Form Data in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template