Python Forum
Help with passing command line input to HTTP POST json - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help with passing command line input to HTTP POST json (/thread-16379.html)



Help with passing command line input to HTTP POST json - sf05wrx - Feb-25-2019

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)



RE: Help with passing command line input to HTTP POST json - moveax3 - Feb-25-2019

You need to replace this:
datas = "{\"MemberGids\": [{\"v\": \"" + pidResponse + ":" + dbidResponse + "\"}]}" 
to this:
datas = {"MemberGids": [{"v": pidResponse + ":" + dbidResponse }]}



RE: Help with passing command line input to HTTP POST json - sf05wrx - Feb-25-2019

(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!