我正在嘗試將我的 curl 轉換為 python 代碼,不幸的是沒有任何成功。誰能幫我解決這個問題,好嗎?
curl -k -X "POST" "https://192.168.16.220:9000/api/views/search/messages" \
-H 'X-Requested-By: superman' \
-H 'Content-Type: application/json' \
-H 'Accept: text/csv' \
-u 'admin:admin' \
-d $'{
"streams": [
"62948e1fcd664d57cccfa29c"
],
"query_string": {
"type": "elasticsearch",
"query_string": "source"
},
"timerange": {
"type": "relative",
"range": 30
}
}'
uj5u.com熱心網友回復:
這應該可以,我也建議將來使用這個方便的網站
import requests
headers = {
'X-Requested-By': 'superman',
# Already added when you pass json= but not when you pass data=
# 'Content-Type': 'application/json',
'Accept': 'text/csv',
}
json_data = {
'streams': [
'62948e1fcd664d57cccfa29c',
],
'query_string': {
'type': 'elasticsearch',
'query_string': 'source',
},
'timerange': {
'type': 'relative',
'range': 30,
},
}
response = requests.post('https://192.168.16.220:9000/api/views/search/messages', headers=headers, json=json_data, verify=False, auth=('admin', 'admin'))
# Note: json_data will not be serialized by requests
# exactly as it was in the original request.
#data = '{\n "streams": [\n "62948e1fcd664d57cccfa29c"\n ],\n "query_string": {\n "type": "elasticsearch",\n "query_string": "source"\n },\n "timerange": {\n "type": "relative",\n "range": 30\n }\n}'
#response = requests.post('https://192.168.16.220:9000/api/views/search/messages', headers=headers, data=data, verify=False, auth=('admin', 'admin'))
像這樣從 api 獲取 json 資料的資料
json_data = response.json()
或者像這樣從 api 獲取文本資料的資料
json_data = response.text
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/484412.html
標籤:python-3.x 卷曲 pycurl
