Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python-requests equivalent to curl's --data-binary?

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.

like image 286
paragbaxi Avatar asked Nov 03 '22 18:11

paragbaxi


1 Answers

Turns out the headers I was supposed to have is

headers = {'X-Requested-With': 'Python requests', 'Content-type': 'text/xml'}
like image 191
paragbaxi Avatar answered Nov 15 '22 04:11

paragbaxi