我正在开发一个可以与Curl一起工作的API,命令看起来像这样:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'api_key: someKey' -d '{
"configurationPath": "/a/b",
"entityTypesFilter": [
"Filter1"
],
"pageSize": 10
}' 'http://localhost:123/ab/rest/v1/entities'
如何将其转换为带有请求库的Python 2代码?
我试过这样的方法:
import requests
headers = {'api_key': 'someKey'}
url = "http://localhost:123/ab/rest/v1/entities"
data = {
"configurationPath": "/a/b",
"entityTypesFilter": [
"Filter1"
],
"pageSize": 10
}
r = requests.post(url, headers=headers, data = data)
print r.content
但这给出了415错误:
> Status Report</p><p><b>Message</b> Unsupported Media Type</p><p><b>Description</b> The origin server is refusing to se
rvice the request because the payload is in a format not supported by this method on the target resource.</p><hr class="
line" /><h3>Apache Tomcat/9.0.8</h3></body></html>
如何修复它?我相信它是一个data
部件的格式,但不确定预期是什么,以及如何修改才能使其工作。
谢谢。
发布于 2018-06-18 19:56:25
curl命令将数据作为JSON与application/json content-type一起发送;Python代码不会这样做。
如果您使用json
参数而不是data
参数,则请求将执行此操作
r = requests.post(url, headers=headers, json=data)
https://stackoverflow.com/questions/50908869
复制相似问题