Python使用内容请求POST JSON数据
py请求:
# 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,默认的content-type = application / x-www-form-urlencoded。 而PHP默认$ _POST只存储值:
在请求中使用application / x-www-form-urlencoded或multipart / form-data作为HTTP Content-Type时,通过HTTP POST方法传递给当前脚本的关联数组。
http://php.net/manual/en/reserved.variables.post.php
所以,我也希望使用Python请求和jQuery相同的默认行为,我该怎么办?
PHP将只接受带有方括号的变量的多个值,表示一个数组(参见本FAQ条目)。
  所以你需要让你的python脚本发送a=1&b[]=2&b[]=3&b[]=4 ,然后$_POST在PHP端看起来像这样: 
[ 'a' => 1, 'b' => [ 2, 3, 4] ] 
上一篇: Python request POST json data using content
下一篇: Pass username and password to Flask Oauth2 Server (password grant type)
