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

Swift HTTP POST请求无法被PHP API正常解析,JSON格式问题排查

Got it, let's fix this JSON POST request issue with your PHP API. The problem here is almost certainly that your request isn't sending the JSON payload correctly, or your PHP script isn't parsing it properly. Let's break this down step by step:

Step 1: Build the Correct Client-Side Request

The key here is twofold: you need to set the right request header, and send a valid JSON object containing your request and signature fields as the request body.

cURL Example

curl -X POST https://your-api-endpoint.php \
  -H "Content-Type: application/json" \
  -d '{
    "request": "your-request-data-here",
    "signature": "your-generated-signature-here"
  }'

JavaScript (Fetch API) Example

fetch('https://your-api-endpoint.php', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    request: 'your-request-data-here',
    signature: 'your-generated-signature-here'
  })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

Python (requests) Example

import requests
import json

url = 'https://your-api-endpoint.php'
payload = {
    'request': 'your-request-data-here',
    'signature': 'your-generated-signature-here'
}
headers = {'Content-Type': 'application/json'}

response = requests.post(url, data=json.dumps(payload), headers=headers)
print(response.json())
Step 2: Update Your PHP Script to Parse JSON Correctly

PHP doesn't automatically parse JSON request bodies into the $_POST array by default. You need to manually read and decode the raw input:

// Read the raw JSON payload from the request
$rawInput = file_get_contents('php://input');
$requestData = json_decode($rawInput, true);

// Validate required fields exist
if (!isset($requestData['request']) || !isset($requestData['signature'])) {
    http_response_code(400);
    echo json_encode(['error' => 'Missing required fields: request or signature']);
    exit;
}

// Now you can use the fields normally
$requestContent = $requestData['request'];
$requestSignature = $requestData['signature'];

// Process your request logic here...
echo json_encode(['status' => 'success', 'message' => 'Request received and validated']);
Common Mistakes to Double-Check
  • Missing Content-Type header: Without setting Content-Type: application/json, your PHP script won't recognize the body as JSON, leading to empty or unparsed data.
  • Invalid JSON syntax: Ensure your payload uses double quotes for keys and strings, no trailing commas, and valid structure. Use a JSON validator if you're unsure.
  • Case-sensitive field names: Make sure request and signature are spelled exactly the same (case included) in both your client payload and PHP script.

内容的提问来源于stack exchange,提问作者Asher Straubing

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:01:52