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

如何在HTTP请求中传递POST数据?代码执行遇body参数读取失败错误

Hey there, let's sort out this POST data issue you're hitting. That error message makes it clear: the server expects a JSON-formatted request body that includes a required requestType parameter, but your current request isn't sending that correctly. Let's break down how to fix this across common tools and languages:

1. Quick test with curl

If you want to verify the API works first, use curl to send a properly formatted JSON request:

curl -X POST \
  https://your-api-endpoint.com/target-path \
  -H "Content-Type: application/json" \
  -d '{
    "requestType": "your-specific-type",
    "optionalParam": "any-extra-data"
  }'

Key notes here:

  • The -H flag sets the Content-Type header to application/json—this tells the server to parse the body as JSON.
  • The -d flag contains your JSON payload, which must include the requestType field with a valid value.
2. Python with requests library

If you're using Python, the requests library makes this straightforward. Here's the correct approach:

import requests

url = "https://your-api-endpoint.com/target-path"
payload = {
    "requestType": "your-specific-type",
    # Add any other required parameters here
}

# The `json=payload` argument automatically sets the Content-Type header to application/json
response = requests.post(url, json=payload)

print("Status code:", response.status_code)
print("Response:", response.json())

Avoid the common mistake of using data=payload without converting it to JSON first—using json=payload handles serialization and headers for you.

3. Browser-side JavaScript with fetch

For front-end code, use fetch to send a JSON POST request like this:

const apiUrl = 'https://your-api-endpoint.com/target-path';
const requestBody = {
  requestType: 'your-specific-type',
  // Include other parameters as needed
};

fetch(apiUrl, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(requestBody),
})
.then(response => response.json())
.then(data => console.log('Success:', data))
.catch(error => console.error('Error:', error));

Important: You must use JSON.stringify() to convert your JavaScript object into a JSON string, and explicitly set the Content-Type header.

Common mistakes to check
  • Missing Content-Type header: Without application/json, the server won't parse your body as JSON, so it can't find requestType.
  • Invalid JSON syntax: Make sure you use double quotes for keys/values (JSON doesn't allow single quotes) and fix any missing commas or brackets.
  • Typos in requestType: The parameter name is case-sensitive—double-check you're using the exact spelling the server expects (e.g., not RequestType or requesttype).
  • Using form data instead of JSON: If you're sending application/x-www-form-urlencoded data, the server won't look for JSON fields like requestType.

内容的提问来源于stack exchange,提问作者Nevis Franko

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:41:56