使用Python Requests发送multipart/form-data报500错误求助
Hey there, let's figure out why your Python Requests code is throwing a 500 error when Postman works perfectly—this is usually down to tiny differences in how the two tools construct the multipart/form-data request. Here are the most common fixes to try:
1. Steal Postman's Generated Code (Yes, Really!)
Postman has a built-in feature to generate Python Requests code that matches exactly what it sends. This is the fastest way to spot gaps in your code:
- In Postman, after your successful request, click the Code button (top-right corner, looks like
</>). - Select Python - Requests from the dropdown menu.
- Copy this generated code and run it directly. If it works, compare it line-by-line with your original code to see what you missed—maybe a header, a field name, or a file MIME type.
2. Double-Check Your File & Field Formatting
Multipart requests are picky about field names, file details, and placement:
- Field names must match exactly: If Postman uses
profile_imageas the file field name, your code can't useProfileImage(case sensitivity matters!). - Specify correct MIME types: When defining files in Requests, include the MIME type Postman uses. For example:
files = { "profile_image": ("me.jpg", open("me.jpg", "rb"), "image/jpeg") } - Separate form data from files: Non-file form fields (like
usernameordescription) go in thedataparameter, notfiles. Mixing them up can confuse the server.
3. Debug the Raw Request Differences
If the generated code doesn't fix it, compare the raw request content from both tools to spot discrepancies:
- Enable debug logging in Requests: Add this code before your request to print every detail of what's being sent:
import requests import logging logging.basicConfig(level=logging.DEBUG) requests_log = logging.getLogger("requests.packages.urllib3") requests_log.setLevel(logging.DEBUG) requests_log.propagate = True - Check Postman's raw request: Open Postman's Console (bottom-left), send the request, and look for the "Raw" tab to see the exact headers and body Postman sends.
- Compare the two outputs—look for missing headers (like
AuthorizationorUser-Agent), incorrect boundary strings, or malformed field values.
4. Don't Forget Cookies/Session Data
Postman might be reusing cookies from a previous login (like a session ID) that your Python code isn't including. Use requests.Session() to automatically handle cookies:
session = requests.Session() # First, send a login request to get authenticated cookies session.post("https://your-server.com/login", data={"username": "your-user", "password": "your-pass"}) # Then send your multipart request using the same session response = session.post("https://your-server.com/upload", data=data, files=files)
5. Watch for Special Characters or Encoding
If your form fields include non-ASCII characters (like Chinese text), make sure Requests is encoding them the same way Postman does. Postman defaults to UTF-8, so ensure your Python strings are encoded correctly—you can explicitly encode values if needed:
data = { "description": "我的照片".encode("utf-8") }
If none of these work, share the raw request logs from both Requests and Postman, and we can dig deeper!
内容的提问来源于stack exchange,提问作者coder

