我从 csv 文件创建了一个 json 文件。这个 JSON 文件有多个请求。我需要对这些请求调用 REST API。如何将这些请求一一发送给API进行处理?我试图搜索,但在任何地方我都只能看到一个请求的语法。不确定我们是否可以批量传递请求。
我不确定如何从文件中选择一个请求并将其作为 POST 方法传递给 API。这是我在搜索中发现的传递 POST 请求的内容:
import requests
r=requests.post(url, data=<how to pass requests one by one here>)
我想我需要先解析这个 JSON 文件。我可以使用这样的东西来解析它。
import json
with open('data.json') as file:
data = json.load(file)
示例 json 文件: data.json
[
{
"consumerData": {
"clientCode": "CSP_CLINICAL",
"instance": "CSP-GFS",
"userId": ""
},
"gfsauthdata": [
{
"Amt": "1000.00",
"FromDt": "2019-02-01T00:00:00",
"Id": "123545",
"LineNo": "1",
"ReceivedDt": "2019-02-01T00:00:00",
"ToDate": "2019-02-01T00:00:00",
"regionId": "WA12"
},
{
"Amt": "200.00",
"FromDt": "2019-02-01T00:00:00",
"Id": "123545",
"LineNo": "2",
"ReceivedDt": "2019-02-01T00:00:00",
"ToDate": "2019-02-01T00:00:00",
"regionId": "WA12"
},
{
"Amt": "200.00",
"FromDt": "2019-02-01T00:00:00",
"Id": "123545",
"LineNo": "3",
"ReceivedDt": "2019-02-01T00:00:00",
"ToDate": "2019-02-01T00:00:00",
"regionId": "WA12"
}
]
},
{
"consumerData": {
"clientCode": "CSP_CLINICAL",
"instance": "CSP-GFS",
"userId": ""
},
"gfsauthdata": [
{
"Amt": "200.00",
"FromDt": "2019-02-01T00:00:00",
"Id": "123546",
"LineNo": "1",
"ReceivedDt": "2019-02-01T00:00:00",
"ToDate": "2019-02-01T00:00:00",
"regionId": "WA13"
},
{
"Amt": "200.00",
"FromDt": "2019-02-01T00:00:00",
"Id": "123546",
"LineNo": "2",
"ReceivedDt": "2019-02-01T00:00:00",
"ToDate": "2019-02-01T00:00:00",
"regionId": "WA13"
}
]
},
{
"consumerData": {
"clientCode": "CSP_CLINICAL",
"instance": "CSP-GFS",
"userId": ""
},
"gfsauthdata": [
{
"Amt": "200.00",
"FromDt": "2019-02-01T00:00:00",
"Id": "123547",
"LineNo": "1",
"ReceivedDt": "2019-02-01T00:00:00",
"ToDate": "2019-02-01T00:00:00",
"regionId": "WA14"
}
]
}
]
我假设您列表的顶部元素是“请求”,因此这段代码应该可以帮助您入门
import json
import requests
with open('data.json') as file:
data = json.load(file)
for element in data:
r=requests.post(url, json=element)
r.raise_for_status() # Add this line to make sure requests succeeded
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句