卷曲到 python 脚本问题

curl to python scripting issue

我正在尝试将我的 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
  }
}'

这应该有用,我也建议以后使用 this 方便的网站

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