从请求中的文件发送json

时间:2019-08-08 09:39:59

标签: python json python-requests

我正在尝试将json文件发送到url(发布)。我正在使用data参数打开文件并将其发送到url:

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

现在可以使用,但已被要求使用“数据”的“ json”指令发送文件。

我已经看到一些使用json创建为字典的示例,该示例可以正常工作,但是我无法从文件中使它工作。

我尝试直接使用它:

json=open(f, 'rb')

在f中,我具有到json文件的路由。 使用json.dumps(open ....序列化json文件。但是我总是收到一条消息,告诉我

1 个答案:

答案 0 :(得分:1)

尝试使用json模块从文件中加载JSON数据。

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)