5

Curl 可以使用--data-binary 选项按原样发送文件。

测试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 包中。

4

1 回答 1

2

原来我应该有的标题是

headers = {'X-Requested-With': 'Python requests', 'Content-type': 'text/xml'}
于 2013-07-16T15:23:50.843 回答