请求模块:将数据发送到远程主机,错误的值为sen

2024-09-28 05:28:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一根弦:

{
    'singleQuesResponses': {
        '28': '',
        '14': '',
        '27': '',
        '26': ''
    },
    'org_apache_struts_taglib_html_TOKEN': 'a1553f25303435076412b5ca7299b936',
    'quesResponses': {
        'some': 'data'
    }
}

当我把它贴在pyhton的请求中时,比如:

^{pr2}$

那么这个帖子的价值是这样的:

array(3) { ["quesResponses"]=> string(4) "some" ["singleQuesResponses"]=> string(2) "14" ["org_apache_struts_taglib_html_TOKEN"]=> string(32) "a1553f25303435076412b5ca7299b936" }

但我希望:

array(3) { ["quesResponses"]=> array["some"=>'data'] ["singleQuesResponses"]=> string(2) "14" ["org_apache_struts_taglib_html_TOKEN"]=> string(32) "a1553f25303435076412b5ca7299b936" }

我的意思是为什么“some”不以数组的形式通过值发送,而它的唯一第一个键作为字符串发送?在


Tags: orgtokenpyhtondatastringapachehtmlsome
2条回答

这就是它的工作原理。将数据参数设置为dict将使请求模块尝试发送它:

Content-Type: application/x-www-form-urlencoded

它只接受字典的第一级并在正文中编码为x=data&y=other\u数据。在

你应该用json.dumps文件(data)或直接将其分配给json参数。在

^{pr2}$

这会将Content-Type头设置为application/json

或者

requests.post(url, data=json.dumps(data))

这不会设置内容类型标头。在

在这种情况下,唯一的方法是用Get发送数据,而不是用数据发布它们;您可以在请求模块中使用params。在

相关问题 更多 >

    热门问题