Python 请求:
# coding=utf-8
from __future__ import print_function
import requests
headers = {
# 'content-type': 'application/json',
'content-type': 'application/x-www-form-urlencoded',
}
params = {
'a': 1,
'b': [2, 3, 4],
}
url = "http://localhost:9393/server.php"
resp = requests.post(url, data=params, headers=headers)
print(resp.content)
PHP接收:
// get HTTP Body
$entityBody = file_get_contents('php://input');
// $entityBody is: "a=1&b=2&b=3&b=4"
// get POST
$post = $_POST;
// $post = ['a' => 1, 'b' => 4]
// $post missing array item: 2, 3
因为我也使用jQuery Ajax POST,其默认内容类型为application/x-www-form-urlencoded。而PHP默认的$_POST仅存储值:
使用HTTP POST方法通过application/x-www-form-urlencoded或multipart/form-data作为请求中的HTTP Content-Type时,传递到当前脚本的变量的关联数组。
http://php.net/manual/en/reserved.variables.post.php
因此,我想要与jQuery默认行为相同地使用Python请求,我该怎么做?