如何使用请求模块使用 Python 将 JSON 文件的内容发布到 RESTFUL API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28259697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to POST contents of JSON file to RESTFUL API with Python using requests module
提问by Jeff F
Okay, I give up. I am trying to post the contents of a file that contains JSON. The contents of the file look like this:
好吧,我放弃了。我正在尝试发布包含 JSON 的文件的内容。该文件的内容如下所示:
{
"id”:99999999,
"orders":[
{
"ID”:8383838383,
"amount":0,
"slotID":36972026
},
{
"ID”:2929292929,
"amount":0,
"slotID":36972026
},
{
"ID”:4747474747,
"amount":0,
"slotID":36972026
}]
}
Here's the code which is probably way off the mark:
这是可能离谱的代码:
#!/usr/bin/env python3
import requests
import json
files = {'file': open(‘example.json', 'rb')}
headers = {'Authorization' : ‘(some auth code)', 'Accept' : 'application/json', 'Content-Type' : 'application/json'}
r = requests.post('https://api.example.com/api/dir/v1/accounts/9999999/orders', files=files, headers=headers)
采纳答案by Hyman
This should work, but it's meant for very large files.
这应该有效,但它适用于非常大的文件。
import requests
url = 'https://api.example.com/api/dir/v1/accounts/9999999/orders'
headers = {'Authorization' : ‘(some auth code)', 'Accept' : 'application/json', 'Content-Type' : 'application/json'}
r = requests.post(url, data=open('example.json', 'rb'), headers=headers)
If you want to send a smaller file, send it as a string.
如果要发送较小的文件,请将其作为字符串发送。
contents = open('example.json', 'rb').read()
r = requests.post(url, data=contents, headers=headers)
回答by salmanwahed
First of all your json file does not contain valid json. as in,"id”
-here the closing quotation mark is different than the opening quotation mark. And other ID fields have the same error. Make it like this "id"
.
首先,您的 json 文件不包含有效的 json。如,-这里的"id”
右引号与左引号不同。和其他ID字段也有同样的错误。让它像这样"id"
。
now you can do it like this,
现在你可以这样做,
import requests
import json
with open('example.json') as json_file:
json_data = json.load(json_file)
headers = {'Authorization' : ‘(some auth code)', 'Accept' : 'application/json', 'Content-Type' : 'application/json'}
r = requests.post('https://api.example.com/api/dir/v1/accounts/9999999/orders', data=json.dumps(json_data), headers=headers)
回答by Ray Hulha
You need to parse the JSON, and pass that the body like so:
您需要解析 JSON,并像这样传递正文:
import requests
import json
json_data = None
with open('example.json') as json_file:
json_data = json.load(json_file)
auth=('token', 'example')
r = requests.post('https://api.example.com/api/dir/v1/accounts/9999999/orders', json=json_data, auth=auth)
回答by Kedar Sadhu
I have done below code while learning Open API and works fine for me.
我在学习 Open API 时完成了以下代码,对我来说效果很好。
`
import requests
url="your url"
json_data = {"id":"k123","name":"abc"}
resp = requests.post(url=url,json=json_data)
print(resp.status_code)
print(resp.text)
`