0

Trying to post json to the specified URL using requests:

import requests, glob, unicodedata, ntpath, time, json

url = 'https://myWebsite.com/ext/ext/ext'

json_file = open("/Users/ME/Documents/folder/folder/test.json")

headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}

r = requests.post(url, data=json.dumps(json_file), headers=headers)

This gives me the following error, however:

TypeError: <open file '/Users/ME/Documents/folder/folder/test.json', mode 'r' at 0x1021c9660> is not JSON serializable

What causes this error? I know my json file is valid json so I don't think that is the problem.

0

2 Answers 2

4

You don't need to produce JSON if you already have valid JSON text in a file. Just send the file contents:

r = requests.post(url, data=json_file, headers=headers)

This will send the file contents of the open file as a POST body, streaming the data efficiently.

json.dumps() is only needed if you have Python objects (such as strings, numbers, booleans, dictionaries, lists, etc.) that need to be represented as a JSON string.

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

1 Comment

It sounds useful to me.
2

open does not return the content of the file, but a file object. try calling read() on the returned value of open to get the actual contents.

See your own error:

TypeError: <open file '/Users/ME/Documents/folder/folder/test.json', mode 'r' at 0x1021c9660> is not JSON serializable

It says that you're passing an OPEN FILE instead of an array of values. Besides, if you want to open the file and send it's contents, the file is NOT a json object so json.dumps() will reencode your content.

If you want to validate your json code prior to sending it, you should:

text = open("your/file.json").read()
try:
    validated = json.dumps(json.loads(text))
    r = requests.post(url, data=validated, headers=headers)
except TypeError as e:
    #blablabla

7 Comments

You don't need to call .read() at all in this case. data takes a file object, it'll be streamed as an efficient upload.
Yes it does, but json.dumps does not expect a file as parameter. that's why I meant to use .read() for json. after that, i suggested a json revalidation
Nope, but that's just a misunderstanding what json.dumps() is for.
Yes, but that's a different error. My suggested solution was because I thought he wanted to revalidate the json content in the file.
The OP writes: I know my json file is valid json so I don't think that is the problem.
|

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.