python-requests equivalent to curl's --data-binary?
python, python-requests, rest
Solution
Turns out the headers I was supposed to have is
headers = {'X-Requested-With': 'Python requests', 'Content-type': 'text/xml'}
Problem
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.