0

I have a question about the requests library in Python.

I tried to send this to my nodeJS server:

payload={
  "sid": _set["_id"],
  "pid": _project["_id"],
  "inputs": [_file["_id"]]  
}
print(payload)
response = requests.post(URLPORT+"/job/",headers={'Authorization': token},data=payload)
_job=response.json()
print(_job)

But what was recived is this (console.log(req.body)):

{ sid: '5a8862e8514580739235c0ab',
  pid: '5a9be32fdacc495d6a2ea8bf',
  inputs: '5a9be342dacc495d6a2ea8c0' }

If I send *"inputs": [_file["_id"],-1]* I get *inputs: [ '5a9be342dacc495d6a2ea8c0', '-1' ] }*

Where are this both guys **[** and **]**, if I send only one element?

Thanks

ndmeiri
  • 4,728
  • 12
  • 32
  • 41
pats
  • 85
  • 1
  • 6

1 Answers1

1

If you want to send a JSON body using requests, use the json argument instead of data. This works:

requests.post("https://httpbin.org/post", json={"inputs": [1, 2, 3]})

Background:

data uses the request content type application/x-www-form-urlencoded which for a list with a single item is identical to a scalar item.

json uses the request content type application/json which serializes a dict/list structure to a JSON object.