You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python requests请求返回500错误:复制HTTP Headers仍未解决

Troubleshooting the 500 Error with Python Requests to Metagenomics Portal

Let’s break down what’s likely going wrong with your code and how to fix it step by step:

1. You’re using files=payload instead of data=payload

Your payload contains regular form field values (like program, method, etc.), not binary file content. The files parameter in Requests is reserved for uploading actual files, while standard form data should be passed via the data parameter. Using files here is almost certainly confusing the server’s form parser, which is triggering the 500 error.

2. Unnecessary DevTools header is raising red flags

The X-DevTools-Emulate-Network-Conditions-Client-Id header is specific to Chrome’s DevTools network emulation tool. Servers often flag requests with this header as non-genuine browser traffic. Remove it from your headers list entirely.

3. Missing session cookies

When you visit the site in Chrome, your browser automatically stores and sends session cookies to maintain a valid session. Requests doesn’t do this by default—so the server might not recognize your POST request as part of a legitimate session. Use a requests.Session() object to persist cookies between your initial GET (to load the application page) and your POST request.

4. Potential confusion with file uploads

If samplefile is supposed to be an actual blast result file you’re uploading, you need to split your data into two parts:

  • Regular form fields go into data
  • The file itself goes into the files parameter

Corrected Code Example (No File Upload, if "blast" is a preset value)

import requests

# Create a session to persist cookies across requests
session = requests.Session()

# First, load the application page to grab session cookies
session.get("http://metagenomics.iiserb.ac.in/mp3/application.php")

payload = {
    "program": "metabin",
    "method": "blast",
    "filetype": "blastresult",
    "samplefile": "blast",
    "bitscore": "-0.2",
    "binsize": "30",
    "email": "",
    "submit": "Submit"
}

headers = {
    "Origin": "http://metagenomics.iiserb.ac.in",
    "Referer": "http://metagenomics.iiserb.ac.in/mp3/application.php",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
}

# Use data=payload for form fields, not files=payload
response = session.post(
    "http://metagenomics.iiserb.ac.in/mp3/submitjob.php",
    data=payload,
    headers=headers
)

# Check the response status and content
print(response.status_code)
print(response.text)

Corrected Code Example (With File Upload)

If you need to upload a blast result file:

import requests

session = requests.Session()
session.get("http://metagenomics.iiserb.ac.in/mp3/application.php")

payload = {
    "program": "metabin",
    "method": "blast",
    "filetype": "blastresult",
    "bitscore": "-0.2",
    "binsize": "30",
    "email": "",
    "submit": "Submit"
}

headers = {
    "Origin": "http://metagenomics.iiserb.ac.in",
    "Referer": "http://metagenomics.iiserb.ac.in/mp3/application.php",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
}

# Pass the file via the files parameter
files = {
    "samplefile": open("your_blast_result.txt", "rb")
}

response = session.post(
    "http://metagenomics.iiserb.ac.in/mp3/submitjob.php",
    data=payload,
    files=files,
    headers=headers
)

print(response.status_code)
print(response.text)

Final Tips

  • Double-check the exact request structure using Chrome’s DevTools Network tab: Inspect the Form Data section of a successful browser submission to ensure your data and files parameters mirror it exactly.
  • Only include headers that are present in a normal browser request—avoid adding niche DevTools-specific headers.

内容的提问来源于stack exchange,提问作者Josh Lewis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:58:21