带有参数数据的 Python 请求发布

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15900338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:20:01  来源:igfitidea点击:

Python Request Post with param data

pythonhttprequestpython-requestshttp-status-codes

提问by slysid

This is the raw request for an API call:

这是 API 调用的原始请求:

POST http://192.168.3.45:8080/api/v2/event/log?sessionKey=b299d17b896417a7b18f46544d40adb734240cc2&format=json HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/json
Content-Length: 86
Host: 192.168.3.45:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

{"eventType":"AAS_PORTAL_START","data":{"uid":"hfe3hf45huf33545","aid":"1","vid":"1"}}"""

This request returns a success (2xx) response.

此请求返回成功 (2xx) 响应。

Now I am trying to post this request using requests:

现在我正在尝试使用requests以下方法发布此请求:

>>> import requests
>>> headers = {'content-type' : 'application/json'}
>>> data ={"eventType":"AAS_PORTAL_START","data{"uid":"hfe3hf45huf33545","aid":"1","vid":"1"}}
>>> url = "http://192.168.3.45:8080/api/v2/event/log?sessionKey=9ebbd0b25760557393a43064a92bae539d962103&format=xml&platformId=1"
>>> requests.post(url,params=data,headers=headers)
<Response [400]>

Everything looks fine to me and I am not quite sure what I posting wrong to get a 400 response.

对我来说一切都很好,我不太确定我发布了什么错误以获得 400 响应。

采纳答案by Martijn Pieters

paramsis for GET-style URL parameters, datais for POST-style body information. It is perfectly legal to provide bothtypes of information in a request, and your request does so too, but you encoded the URL parameters into the URL already.

params用于 GET 样式的 URL 参数,data用于 POST 样式的正文信息。在请求中提供这两种类型的信息是完全合法的,您的请求也是如此,但您已经将 URL 参数编码到 URL 中。

Your raw post contains JSONdata though. requestscan handle JSON encoding for you, and it'll set the correct Content-Headertoo; all you need to do is pass in the Python object to be encoded as JSON into the jsonkeyword argument.

不过,您的原始帖子包含JSON数据。requests可以为您处理 JSON 编码,它也会设置正确的Content-Header;您需要做的就是将要编码为 JSON 的 Python 对象传递到json关键字参数中。

You could split out the URL parameters as well:

您也可以拆分 URL 参数:

params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}

then post your data with:

然后发布您的数据:

import requests

url = 'http://192.168.3.45:8080/api/v2/event/log'

data = {"eventType": "AAS_PORTAL_START", "data": {"uid": "hfe3hf45huf33545", "aid": "1", "vid": "1"}}
params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}

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

The jsonkeyword is new in requestsversion 2.4.2; if you still have to use an older version, encode the JSON manually using the jsonmodule and post the encoded result as the datakey; you will have to explicitly set the Content-Type header in that case:

json关键字是requests2.4.2 版新增的;如果您仍然必须使用旧版本,请使用json模块手动编码 JSON并将编码结果作为data密钥发布;在这种情况下,您必须显式设置 Content-Type 标头:

import requests
import json

headers = {'content-type': 'application/json'}
url = 'http://192.168.3.45:8080/api/v2/event/log'

data = {"eventType": "AAS_PORTAL_START", "data": {"uid": "hfe3hf45huf33545", "aid": "1", "vid": "1"}}
params = {'sessionKey': '9ebbd0b25760557393a43064a92bae539d962103', 'format': 'xml', 'platformId': 1}

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

回答by Noel Evans

Assign the response to a value and test the attributes of it. These should tell you something useful.

将响应分配给一个值并测试它的属性。这些应该告诉你一些有用的东西。

response = requests.post(url,params=data,headers=headers)
response.status_code
response.text
  • status_code should just reconfirm the code you were given before, of course
  • status_code 应该只是重新确认您之前给出的代码,当然

回答by Ionut Hulub

Set data to this:

将数据设置为:

data ={"eventType":"AAS_PORTAL_START","data":{"uid":"hfe3hf45huf33545","aid":"1","vid":"1"}}