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))
3
  • 1
    Post your code and all relevant information...your question is not complete.. Commented Dec 23, 2015 at 14:03
  • Thanks, I have added the code Commented Dec 23, 2015 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. Commented Dec 24, 2015 at 0:44

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

1 Comment

It looks as if the JSON is wrong in the first link. I am guessing mine cannot be wrong if it works in postman

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.