5

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.

3
  • possible duplicate of Python requests - POST data from a file Commented Jul 16, 2013 at 15:25
  • 2
    Your question title is rather misleading; your problem lies with the headers, not with how to emulate --data-binary with requests. The dupe I linked to does ask that question. Commented Jul 16, 2013 at 15:27
  • 2
    And judging from your payload contents and your Content-type header, my immediate response to your posted answer below is Yeah, duh. :-D Commented Jul 16, 2013 at 15:28

1 Answer 1

2

Turns out the headers I was supposed to have is

headers = {'X-Requested-With': 'Python requests', 'Content-type': 'text/xml'}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.