pythonelasticsearchkibanaelasticsearch-py

Convert Kibana query (create index pattern) to Python request


I'm trying to convert this Kibana query to Python:

PUT /.kibana/_doc/index-pattern:tempindex
{
  "type": "index-pattern",
  "index-pattern": {
    "title": "tempindex",
    "timeFieldName": "sendTime"
  }
}

This is what I have so far:

HEADERS = {
    'Content-Type': 'application/json'
}

uri = "http://localhost:5601/_doc/index-pattern:tempindex"

query = json.dumps({
  "type": "index-pattern",
  "index-pattern": {
    "title": "tempindex",
    "timeFieldName": "sendTime"
  }
})

r = requests.put(uri, headers=HEADERS, data=query).json()
print(r)

But it gives me

{'statusCode': 404, 'error': 'Not Found', 'message': 'Not Found'}

What am I doing wrong?

P.S: Both Elastic and Kibana servers are local (Windows 10).


Solution

  • Seems that just changing the uri does the trick:

    uri = "http://localhost:9200/.kibana/_doc/index-pattern:tempindex"
    

    But I'm not sure about the HEADERS, cuz as lupanoide pointed out, kbn-xsrf: true should be present, but either way it seems to be working and apparently the results are the same (I haven't spotted a difference yet).

    Edit: As the doc says:

    kbn-xsrf: true

    By default, you must use kbn-xsrf for all API calls, except in the following scenarios:

    The API endpoint uses the GET or HEAD operations
    The path is whitelisted using the server.xsrf.whitelist setting
    XSRF protections are disabled using the server.xsrf.disableProtection setting

    So I think it should be present.