0

Hey I am trying to import data that is already formatted as JSON data. I am trying to get it to be read in Python so I can use it for a http post request. I have tried saving it as .JSON and .txt and using json.dumps on both files but I'm still getting it in the wrong format. The code is below. I am guessing it is reading in the wrong format as the response from the post is getting an error. However when I use postman for the job, no error.

workingFile = 'D:\\test.json'

file = open(workingFile, 'r')

read = [file.read()]

data = json.dumps(read)
url = 'http://webaddress'
username = 'username'
password = 'password'

requestpost = requests.post(url, data, auth=(username, password))
tosh
  • 509
  • 1
  • 4
  • 9
  • 1
    Post your code and all relevant information...your question is not complete.. – Iron Fist Dec 23 '15 at 14:03
  • Thanks, I have added the code – tosh Dec 23 '15 at 14:08
  • If you don't actually need to modify the JSON before sending it and you know that you're only dealing with valid JSON, then there no reason to parse and dump it. Just send the text as is. – Lie Ryan Dec 24 '15 at 00:44

2 Answers2

1
workingFile = 'D:\\test.json'

with open(workingFile, 'r') as fh:
    data = json.load(fh)

url = 'http://webaddress'
username = 'username'
password = 'password'

requestpost = requests.post(url, json=data, auth=(username, password))

By specifying json=data, requests encodes the payload as json instead of form data

Mukund M K
  • 81
  • 4
0
Community
  • 1
  • 1
Netro
  • 7,119
  • 6
  • 40
  • 58
  • It looks as if the JSON is wrong in the first link. I am guessing mine cannot be wrong if it works in postman – tosh Dec 23 '15 at 14:16