0

I have a unix script which generates a url and executes it :

export url='http://test.com';
export job_name='MY_JOB_NAME';

jso="{\"parameter\": [{\"name\":\"BRANCH\",\"value\":\"master\"}, {\"name\":\"GITURL\",\"value\":\"https://github.test.com/test/test.git\"}]}";

curl $url/job/$job_name/build --data-urlencode json="$jso";

I want to do exactly the same thing in Python and I tried using the 'requests' and 'urllib2' modules, but they don't seem to form the exact same request.

Here is what I have tried :

import requests
import json

url='http://test.com/job/MY_JOB_NAME/build'

params=[{'name':'BRANCH', 'value':'master'}, {'name':'GITURL', 'value':'https://github.test.ebay.com/test/test.git'}]
payload = json.dumps(params)
resp = requests.post(url, data={'json':payload})

Am I doing something wrong here ?

2 Answers 2

2

If we hit a test server with your curl request we see that what you're doing is POSTing a form data field named json with a value of a JSON encoded string.

~$ curl http://httpbin.org/post --data-urlencode json='{"foo": "bar"}'
{
  "url": "http://httpbin.org/post",
  "json": null,
  "args": {},
  "form": {
    "json": "{\"foo\": \"bar\"}"
  },
  "origin": "0.0.0.0",
  "data": "",
  "headers": {
    "Connection": "close",
    "Content-Type": "application/x-www-form-urlencoded",
    "X-Request-Id": "1b5f0122-9e63-4e58-adff-e59c24f086e5",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.30.0",
    "Content-Length": "35",
    "Accept": "*/*"
  },
  "files": {}
}

With that said, the main difference I see between your curl script and your python script is the structure of the JSON encoded data you're posting.

Your curl script is posting this:

{
  "parameters": [{
    "name": "BRANCH",
    "value": "foo",
  },
  {
    "name": "GITURL",
    "value": "git://example.com/repo",
  }]
}

Your requests code is posting

{
  "name": "BRANCH",
  "value": "foo"
}

So you're not posting the same data. If you copy and paste the structure that I and use json.dumps on that with the right data, your call to request.post should work. The rest of it is 100% correct.

Sign up to request clarification or add additional context in comments.

3 Comments

My answer isn't correct, I know. There used to be another answer which the OP agreed with, but it's missing now.
Edited my json payload to match it to the curl json payload. I still don't see the url being executed successfully.
@PiHorse if the edited question is what you tried then you're not sending the right. You have to JSON encode {'parameters': [{...},{...}]}.
-1

Try this:

params={'BRANCH':'master', 'GITURL':'https://github.test.com/test/test.git'}
resp = requests.post(url, data=payload)

See requests:post-a-multipart-encoded-file There is no need to dump your data into json, since

Your dictionary of data will automatically be form-encoded when the request is made

I've used requests a lot, and simply passing a dict would always work.

1 Comment

See my answer. OP is not trying to post a multipart encoded file and simply using the data parameter as you suggested will not work. You didn't read OPs question closely enough and you didn't try to understand exactly what they're doing.

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.