Pi Horse
Pi Horse

Reputation: 2430

Python - Json & Requests

import requests
import json

url='http://test.com/job/MY_JOB_NAME/build'
params={'name':'BRANCH', 'value':'master', 'name':'GITURL', 'value':'https://github.test.com/test/test.git'}
payload = json.dumps(params)
print payload
resp = requests.post(url=url, data=payload)

For some reason the request doesn't get executed successfully, so I print the payload to see what paramaters are being passed as json and I get this:

{"name": "GITURL", "value": "https://github.scm.corp.ebay.com/RPS/RPS.git"}

Why is my payload missing the first 2 json key-value pairs ?

Upvotes: 0

Views: 259

Answers (2)

falsetru
falsetru

Reputation: 369064

It's not the problem of the json.dumps.

In the dict literal, same keys are present. Keys should be unique.

>>> {'a': 'b', 'a': 'c'}
{'a': 'c'}

Use different keys, or make values as list:

>>> {'a1': 'b', 'a2': 'c'}
{'a1': 'b', 'a2': 'c'}
>>> {'a': ['b' ,'c']}
{'a': ['b', 'c']}

or use a list of dictionaries:

>>> [{'a': 'b'}, {'a': 'c'}]
[{'a': 'b'}, {'a': 'c'}]

Upvotes: 2

Torxed
Torxed

Reputation: 23480

You have two identical keys, name and value.

Change the names.

params={'branch':'BRANCH', 'tree':'master', 'name':'GITURL', 'value':'https://github.test.com/test/test.git'}

JSON is just like the dictionary in Python. They are matched on a Key = Value basis, and each key is a uiniqieue identifier.

x = {}
x['elephant'] = 1

And if another elephant comes along,

x['elephant'] += 1

But you can't do:

x['elephant'] = 'has a trunk'

Because, then you replace the number of elephants with how they look.

Upvotes: 1

Related Questions