0

I have this cURL:

curl -X POST http://user:pass@blabla.com:8080/job/myproject/config.xml --data-binary "@new_config.xml"

I am basically trying to set a new config for a Jenkins installation by changing the pre-existing config.xml file. I am trying to convert it to something like this in order to use it more flexibly in my code:

url     = "http://host:8080/job/myproject/config.xml"
auth    = ('user','pass')
payload = {"--data-binary": "@new_config.xml"}
headers = {"Content-Type" : "application/xml"}
r = requests.post(url, auth=auth, data=payload, headers=headers)

I know that I am using incorrectly the payload and the headers.How should I change them? I run it and I take a 500 responce code.

I read this post , but I am struggling to apply it in my case.

Kostas Demiris
  • 3,415
  • 8
  • 47
  • 85

1 Answers1

3

The --data-binary switch means: post the command line argument as the whole POST body, without wrapping in multipart/form-data or application/x-www-form-encoding containers. @ tells curl to load the data from a filename; new_config.xml in this case.

You'll need to open the file object to send the contents as the data argument:

url     = "http://host:8080/job/myproject/config.xml"
auth    = ('user','pass')
headers = {"Content-Type" : "application/xml"}
with open('new_config.xml', 'rb') as payload:
    r = requests.post(url, auth=auth, data=payload, headers=headers)

Note that I pass the file object directly into requests; the data will then be read and pushed to the HTTP socket, streaming the data efficiently.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks Martijn, it did a fine job! If I may ask you, could you please point me to some links to understand better cURL ? I inherited a lot of scripts that use it and I would most like to make the transition to something more elegant like requests. – Kostas Demiris Oct 27 '15 at 18:29
  • I just use the `man curl` help page to understand what the command line does. There is a certain amount of basic HTTP knowledge required to understand both `curl` and `requests` here though. – Martijn Pieters Oct 27 '15 at 18:30
  • I'll start from there then. My best! – Kostas Demiris Oct 27 '15 at 18:32