如何使用请求模块将 JSON 文件的 POST 内容 RESTFUL API 和 Python
how to POST contents of JSON file to RESTFUL API with Python using requests module
好吧,我放弃了。我正在尝试 post 包含 JSON 的文件的内容。文件内容如下所示:
{
"id”:99999999,
"orders":[
{
"ID”:8383838383,
"amount":0,
"slotID":36972026
},
{
"ID”:2929292929,
"amount":0,
"slotID":36972026
},
{
"ID”:4747474747,
"amount":0,
"slotID":36972026
}]
}
下面是可能偏离标准的代码:
#!/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)
这应该可行,但它适用于非常大的文件。
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)
如果要发送较小的文件,请将其作为字符串发送。
contents = open('example.json', 'rb').read()
r = requests.post(url, data=contents, headers=headers)
首先,您的 json 文件不包含有效的 json。如,"id”
- 这里的结束引号与开始引号不同。其他ID字段也有同样的错误。像这样 "id"
.
现在你可以这样做了,
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)
您需要解析 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)
我在学习 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)
`
好吧,我放弃了。我正在尝试 post 包含 JSON 的文件的内容。文件内容如下所示:
{
"id”:99999999,
"orders":[
{
"ID”:8383838383,
"amount":0,
"slotID":36972026
},
{
"ID”:2929292929,
"amount":0,
"slotID":36972026
},
{
"ID”:4747474747,
"amount":0,
"slotID":36972026
}]
}
下面是可能偏离标准的代码:
#!/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)
这应该可行,但它适用于非常大的文件。
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)
如果要发送较小的文件,请将其作为字符串发送。
contents = open('example.json', 'rb').read()
r = requests.post(url, data=contents, headers=headers)
首先,您的 json 文件不包含有效的 json。如,"id”
- 这里的结束引号与开始引号不同。其他ID字段也有同样的错误。像这样 "id"
.
现在你可以这样做了,
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)
您需要解析 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)
我在学习 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)
`