python-requests equivalent to curl's --data-bi

2019-05-09 03:26发布

Curl has an option to send a file as is with the --data-binary option.

When testing the Qualys WAS API, the following curl command works:

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>

Using Python's requests module, I keep receiving a 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'))

When trying to submit file files parameter, it also ended in a 415 error.

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'))

The reason I'm setting this up is to incorporate this into the qualysapi Python package.

1条回答
可以哭但决不认输i
2楼-- · 2019-05-09 04:00

Turns out the headers I was supposed to have is

headers = {'X-Requested-With': 'Python requests', 'Content-type': 'text/xml'}
查看更多
登录 后发表回答