python-请求相当于curl的--data-binary?

时间:2013-07-16 15:23:51

标签: python rest python-requests

Curl可以选择使用--data-binary option发送文件。

测试Qualys WAS API时,以下curl命令有效:

curl -u "username:password" -H "content-type: text/xml" -X "POST" --data-binary @- "https://qualysapi.qualys.com/qps/rest/3.0/search/was/webapp" < post.xml

post.xml:

<ServiceRequest><filters><Criteria operator="CONTAINS" field="name">PB - </Criteria></filters></ServiceRequest>

使用Python的请求模块,我一直收到HTTPError:415 Client Error:Unsupported Media Type。

import requests
url = 'https://qualysapi.qualys.com/qps/rest/3.0/search/was/webapp'
payload = '<ServiceRequest><filters><Criteria operator="CONTAINS" field="name">PB - </Criteria></filters></ServiceRequest>'
headers = {'X-Requested-With': 'Python requests', 'Content-type': 'application/json'}
r = requests.post(url, data=payload, headers=headers, auth=('username', 'password'))

尝试提交文件文件参数时,它也以415错误结束。

import requests
url = 'https://qualysapi.qualys.com/qps/rest/3.0/search/was/webapp'
payload = '<ServiceRequest><filters><Criteria operator="CONTAINS" field="name">PB - </Criteria></filters></ServiceRequest>'
headers = {'X-Requested-With': 'Python requests', 'Content-type': 'application/json'}
r = requests.post(url, data=payload, headers=headers, auth=('username', 'password'))

我设置它的原因是将其纳入qualysapi Python package

1 个答案:

答案 0 :(得分:0)

原来我应该拥有的标题是

headers = {'X-Requested-With': 'Python requests', 'Content-type': 'text/xml'}