Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将PHP中的http_build_查询转换为表单数据的python等价物_Python_Php_Http_Python Requests_Form Data - Fatal编程技术网

如何将PHP中的http_build_查询转换为表单数据的python等价物

如何将PHP中的http_build_查询转换为表单数据的python等价物,python,php,http,python-requests,form-data,Python,Php,Http,Python Requests,Form Data,以下是php代码: 为方便起见: $url = "https://app.sender.net/api/"; $data = array( "method" => "listSubscribe", "params" => [ "api_key" => "98844b0f8da95e7c8ace0aaa6234e4e2",

以下是php代码:

为方便起见:

$url = "https://app.sender.net/api/";

$data = array(
    "method" => "listSubscribe", 
    "params" => [
        "api_key" => "98844b0f8da95e7c8ace0aaa6234e4e2",
        "list_id" => "12345",
        "emails" => [
            "name.lastname@example.com"
        ]
    ]
);

$options = [
    'http' => [
        'method'  => 'POST',
        'content' => http_build_query(array('data' => json_encode($data)))
    ]
];

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
我在python中的尝试:

import requests
myobj = {
    "method": "listSubscribe", 
    "params": {
        "api_key": sender_api_key,
        "list_id": sender_list_id,
        "emails": ["email@email.com"]
    }
}


r = requests.post(sender_api_url, files={'data':json.dumps(myobj)})
我已经尝试了这么多的组合,我继续得到
{“error”:{“code”:“002”,“message”:“Empty request”}
作为响应

我从
r.response
收到一个200,所以它至少被发送了

当我将API密钥更改为无效时,会出现相同的错误


files=
更改为
json=
data=
也不起作用

解决方案是使用

r = requests.post(sender_api_url, data=dict(data=json.dumps(myobj)))