Python Forum
Help with passing command line input to HTTP POST json
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)
Reply
#2
You need to replace this:
datas = "{\"MemberGids\": [{\"v\": \"" + pidResponse + ":" + dbidResponse + "\"}]}" 
to this:
datas = {"MemberGids": [{"v": pidResponse + ":" + dbidResponse }]}
Reply
#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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Receive Input on Same Line? johnywhy 8 606 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  problem in using input command akbarza 4 996 Oct-19-2023, 03:27 PM
Last Post: popejose
  Command line argument issue space issue mg24 5 1,279 Oct-26-2022, 11:05 PM
Last Post: Yoriz
  accept command line argument mg24 5 1,239 Sep-27-2022, 05:58 PM
Last Post: snippsat
  Accessing varying command line arguements Rakshan 3 2,008 Jul-28-2021, 03:18 PM
Last Post: snippsat
  How to input & output parameters from command line argument shantanu97 1 2,505 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  How to make input come after input if certain line inserted and if not runs OtherCode Adrian_L 6 3,270 Apr-04-2021, 06:10 PM
Last Post: Adrian_L
  Printing a specific line from a JSON serpiente 4 3,686 Mar-14-2021, 07:27 PM
Last Post: buran
  Multi-line console input lizze 4 2,283 Dec-26-2020, 08:10 AM
Last Post: lizze
  Passing List of Objects in Command Line Python usman 7 3,084 Sep-27-2020, 03:45 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020