带有参数数据的 Python 请求发布 [英] Python Request Post with param data

查看:24
本文介绍了带有参数数据的 Python 请求发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 API 调用的原始请求:

POST http://192.168.3.45:8080/api/v2/event/log?sessionKey=b299d17b896417a7b18f46544d40adb734240cc2&format=json HTTP/1.1接受编码:gzip,deflate内容类型:应用程序/json内容长度:86主持人:192.168.3.45:8080连接:保持活动用户代理:Apache-HttpClient/4.1.1 (java 1.5){"eventType":"AAS_PORTAL_START","data":{"uid":"hfe3hf45huf33545","aid":"1","vid":"1"}}"""

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

现在我尝试使用 requests 发布此请求:

<预><代码>>>>进口请求>>>标头 = {'内容类型':'应用程序/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)<响应[400]>

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

解决方案

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

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

您也可以拆分 URL 参数:

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

然后发布您的数据:

导入请求url = 'http://192.168.3.45:8080/api/v2/event/log'数据 = {eventType":AAS_PORTAL_START",数据":{uid":hfe3hf45huf33545",aid":1",vid":}1"}参数 = {'sessionKey':'9ebbd0b25760557393a43064a92bae539d962103','格式':'xml','platformId':1}requests.post(url, params=params, json=data)

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

导入请求导入json标题 = {'内容类型':'应用程序/json'}url = 'http://192.168.3.45:8080/api/v2/event/log'数据 = {eventType":AAS_PORTAL_START",数据":{uid":hfe3hf45huf33545",aid":1",vid":}1"}参数 = {'sessionKey':'9ebbd0b25760557393a43064a92bae539d962103','格式':'xml','platformId':1}requests.post(url, params=params, data=json.dumps(data), headers=headers)

This is the raw request for an API call:

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.

Now I am trying to post this request using 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.

解决方案

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

Your raw post contains JSON data though. requests can handle JSON encoding for you, and it'll set the correct Content-Type header too; all you need to do is pass in the Python object to be encoded as JSON into the json keyword argument.

You could split out the URL parameters as well:

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 json keyword is new in requests version 2.4.2; if you still have to use an older version, encode the JSON manually using the json module and post the encoded result as the data key; you will have to explicitly set the Content-Type header in that case:

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)

这篇关于带有参数数据的 Python 请求发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆