Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

curl to python scripting issue

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading