Python Forum

Full Version: Help with passing command line input to HTTP POST json
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to do a HTTP POST request in which I take in input from the user for the json. However, when I use the input from the user my POST doesn't work (500 error), but when I hardcoded the data it does work (200).


import sys
import json
import requests

pidResponse = raw_input("Enter PersonID: ");
dbidResponse = raw_input("Enter DBID: ");

url = "http://some_url"

#This doesn't work
datasUSERINPUT = "{\"MemberGids\": [{\"v\": \"" + pidResponse + ":" + dbidResponse + "\"}]}" 

#This does work
#datasHARDCODED = {"MemberGids": [{"v": "137624192:7884"}]}
headers = {'Content-type': 'application/json'}
rsp = requests.post(url, json=datas, headers=headers)

print(datas)
print(rsp)
print(rsp.text)
You need to replace this:
datas = "{\"MemberGids\": [{\"v\": \"" + pidResponse + ":" + dbidResponse + "\"}]}" 
to this:
datas = {"MemberGids": [{"v": pidResponse + ":" + dbidResponse }]}
(Feb-25-2019, 11:15 PM)moveax3 Wrote: [ -> ]You need to replace this:
datas = "{\"MemberGids\": [{\"v\": \"" + pidResponse + ":" + dbidResponse + "\"}]}" 
to this:
datas = {"MemberGids": [{"v": pidResponse + ":" + dbidResponse }]}

Thanks, that worked!