0

I am trying to send a json file to a url (post). I am using data parameter to open the file and send it to the url:

r = requests.post(url_info, data=open(f, 'rb'), headers=headers, verify=False)

This works right now but I have been asked to use "json" insteand of "data" to send the file.

I have seen some examples using json created as dictionaries where this is working but I am not able to make it work from a file.

I have tried to use it directly:

json=open(f, 'rb')

In f I have the route to the json file. Serialize the json file with json.dumps(open.... But I always get a message telling me that

1 Answer 1

1

Try using the json module to load JSON data from the file.

import requests
import json

# open the file and load JSON data from it
with open(f, "r") as file:
    data = json.load(f)  # type(data) -> <class 'dict'>
# send POST request with the loaded data
r = requests.post(url_info, data=data, headers=headers, verify=False)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Tony.C I just changed data=data to json=data and it worked!

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.