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
  I am a newbie I like to use the command line Mikel2025 1 1,063 Jun-13-2025, 03:20 PM
Last Post: Gribouillis
  Βad Input on line 12 Azdaghost 5 2,237 Apr-19-2025, 10:22 PM
Last Post: Azdaghost
  Insert command line in script lif 4 2,200 Mar-24-2025, 10:30 PM
Last Post: lif
  How to revert back to a previous line from user input Sharkenn64u 2 3,184 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  Simplest way to run external command line app with parameters? Winfried 2 2,360 Aug-19-2024, 03:11 PM
Last Post: snippsat
  Receive Input on Same Line? johnywhy 8 3,794 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  problem in using input command akbarza 4 4,585 Oct-19-2023, 03:27 PM
Last Post: popejose
  Command line argument issue space issue mg24 5 3,167 Oct-26-2022, 11:05 PM
Last Post: Yoriz
  accept command line argument mg24 5 3,141 Sep-27-2022, 05:58 PM
Last Post: snippsat
  Accessing varying command line arguements Rakshan 3 3,742 Jul-28-2021, 03:18 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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