简体   繁体   中英

how to get double-quoted json body in python?

I have such code in python3 to try to load the json data from "sample.json" file, and then to make a post REST API call.

import json

with open('sample.json' ) as request_file:
        post_data = json.load(request_file)
print(requst_data)

headers = {'X-CSRFToken': '...', 'Cookie': '...', 'Content-Type': 'application/json', 'Referer': MY_URL}
response = requests.post(MY_URL, data=request_data, headers=headers)
print(response.json())

But the api call failed for

{'msg': ['JSON parse error - Expecting value: line 1 column 1 (char 0)']}

What I have realized is in the "sample.json", everything is double-quoted, but after this json.load() call, the "request_data" is of single-quoted json value.

Is there anyway I can work around this issue?

I don't know why, but if I to update the header as this:

headers = {'X-CSRFToken': '...', 'Cookie': '...', 'Referer': MY_URL}

which is to remove the 'Content-Type': 'application/json' part, the call works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM