尝试运行curl命令来获取响应并将其保存到文件中
无法创建代码,因为这是第一次尝试在 python 中使用curl 命令,不知道从哪里开始
curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "
{ \"Products\": [], \"DataProducts\": [], \"includeExtractFields\": true, \"includedDocumentTypes\": [], \"removeOrphans\": true, \"searchDataProduct\": \"Model|test\", \"searchField\": \"ID_TEST\", \"searchValues\": [ \"123456789\",\"987654321\" ] }
" "http://test"
Run Code Online (Sandbox Code Playgroud)
curl 命令应返回 json,然后将其保存到文件中
您应该考虑在本机 Python 中执行此操作,而不是在外部执行curl。以下是如何使用 Python 和requests包发出 POST 请求的示例:
import requests
import json
response = requests.post('https://yoururl', data = {'key':'value'})
with open('output.json', 'w') as f:
json.dump(response.json(), f)
Run Code Online (Sandbox Code Playgroud)
您可以阅读请求文档来读取/写入标头等。
对于您的具体情况:
import requests
import json
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
data = {
"Products": [],
"DataProducts": [],
"includeExtractFields": True,
"includedDocumentTypes": [],
"removeOrphans": True,
"searchDataProduct": "Model|test",
"searchField": "ID_TEST",
"searchValues": [ "123456789","987654321" ]
}
# To send data form-encoded
response = requests.post('http://test/', headers=headers, data=data)
# To send data json-encoded
response = requests.post('http://test/', headers=headers, json=data)
# Save response as JSON file
with open('output.json', 'w') as f:
json.dump(response.json(), f)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4377 次 |
| 最近记录: |