Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Help with passing command line input to HTTP POST json
#1
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)
Quote
#2
You need to replace this:
datas = "{\"MemberGids\": [{\"v\": \"" + pidResponse + ":" + dbidResponse + "\"}]}" 
to this:
datas = {"MemberGids": [{"v": pidResponse + ":" + dbidResponse }]}
Quote
#3
(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!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Taking Multiple Command Line Argument Input bwdu 6 266 Mar-29-2020, 05:52 PM
Last Post: buran
  python 3 from command line Dixon 1 214 Mar-01-2020, 08:35 PM
Last Post: snippsat
  How to make input goto a different line mxl671 2 365 Feb-04-2020, 07:12 PM
Last Post: Marbelous
  Running linux command line apps... dbrdh 0 185 Jan-30-2020, 01:14 PM
Last Post: dbrdh
  command line input (arg parse) and data exchange Simba 7 639 Dec-06-2019, 11:58 PM
Last Post: Simba
  Multiple Line Input helenaxoxo 4 394 Dec-02-2019, 11:06 PM
Last Post: helenaxoxo
  How to write a script to execute a program need passing additional input? larkypython 2 398 Nov-23-2019, 04:38 AM
Last Post: larkypython
  Post JSON dat and Image vijsi 2 370 Oct-03-2019, 01:35 PM
Last Post: vijsi
  code works at command line but not in .py ridgerunnersjw 4 375 Sep-12-2019, 11:09 PM
Last Post: ridgerunnersjw
  Post JSON from python to PHP don't give expected result pgagnebin 1 427 Sep-04-2019, 10:29 PM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)