Kostas Demiris
Kostas Demiris

Reputation: 3621

How to convert cURL in Python requests to change Jenkins config.xml file

I have this cURL:

curl -X POST http://user:[email protected]: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.

Upvotes: 0

Views: 977

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122182

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.

Upvotes: 3

Related Questions