Home > Software design >  curl to python scripting issue
curl to python scripting issue

Time:05-31

i'm trying to convert my curl to a python code, unfortunately without any success. can anyone help me with this one, please?

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
  }
}'

CodePudding user response:

This should work, also I suggest using this handy site in the future

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'))

get the data from the api like this for json data

json_data = response.json()

or get the data from the api like this for text data

json_data = response.text
  • Related