Question

I'm using the Requests library to PUT data to the Pipeline Deals API, and the data requires that I PUT a list for a value in the request data.

Their example: "custom_label_83": [ 28, 29 ].

This is how I'm PUTting the data:

requests.put("https://www.pipelinedeals.com/...", data={'custom_label_83': [28,29]})

The issue seems to be that when I PUT that example, I end up with only 29 being PUT, because the body of the request (after being encoded) is like this:

custom_label_83%5D=28&custom_fields%5D%5Bcustom_label_83%5D=29

As a result, the field is set twice, with the final result being 29, instead of a list that includes both 28 and 29. I would like the body of the PUT request to be something like this:

custom_label_83%5D=28,29

How do I do that?

Was it helpful?

Solution

The reason for the behaviour you are experiencing (multiple GET parameters) is the default requests behaviour when you pass a list as a value for a key in the params dict.

If you would like the output to be custom_label_83%5D=28,29, you have to join the list's values:

>>> ','.join(map(str, [28, 29]))
'28,29'

OTHER TIPS

If you want 28 and 29 to be together, you'll have to combine them within the required list somehow. You could use a string, nested list, nested tuple etc...

"custom_label_83": [ '28,29' ] #string example

"custom_label_83": [ [28,29] ] #nested list example

"custom_label_83": [ (28,29) ] #nested tuple example
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top