0

Summary: I want to get search results from an Elasticsearch instance via Javascript. The POST call fails, while the GET one is fine.

EDIT: I found the solution through another question (I will flag mine as a duplicate), though I do not understand why the need for JSON.stringify().


Details: I am using the following function to request the total number of (filtered) hits:

function nrVulns() {
    $.ajax({
        type: "POST",
        crossDomain: true,
        url: 'http://elk.example.com:9200/scan/vulnerability/_search',
        data: {
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "risk_factor": "critical"
                            }
            }
         ]
                }
            },
            "size": 0
        }
    }).success(function (data) {
        $('#nrvuln').text(data["hits"]["total"]);

    })
}

It fails with a 400 Bad Request reply from Elasticsearch:

enter image description here

However:

  • the same function with type: "GET" goes though, returning the total number of hits (not filtered by query). It looks like the data section is not taken into account (which is OK for a GET request)
  • the data in the query above, copied and pasted verbatim into Sense and sent though a POST yields correct results (["hits"]["total"] shows the correct number of hits, filtered by query)
  • a JSONLint confirms that the data string is correct JSON.

What is wrong with the POST version of this query?

Community
  • 1
  • 1
WoJ
  • 27,165
  • 48
  • 180
  • 345
  • Are you sure that the API supports a POST request to this endpoint? The Bad Request error indicates that it probably doesn't, or that the data you're providing is incorrect, either in format or value. You would need to refer to the API documentation for more information. There's nothing here that we can diagnose. – Rory McCrossan Feb 09 '16 at 08:23
  • @RoryMcCrossan: this is why I put my "however" part: the data sent from somewhere else than Javascript is OK, the GET part goes though (so the call itself is successful, without the data), and the format of the data is correct (both from a JSON perspective and, as seen above, from an API perspective) – WoJ Feb 09 '16 at 08:25
  • Is there any more information provided in the network console about the 400 error? – Rory McCrossan Feb 09 '16 at 08:27
  • @RoryMcCrossan: yes, this is the reason for the second bullet in my "however" part: I tested a POST request with the same data, which is successful (outside of JS) – WoJ Feb 09 '16 at 08:27
  • 1
    @RoryMcCrossan: I updated my post with the error information from Dev Tools in Chrome – WoJ Feb 09 '16 at 08:30
  • i dont think that this will work -> "No 'Access-Control-Allow-Origin' header is present on the requested resource." this is the response, you can try adding headers . – Vitaliy Terziev Feb 09 '16 at 09:35
  • @vtz: my Elasticsearch instance is CORS compliant (per the [docs](https://www.elastic.co/guide/en/elasticsearch/reference/1.4/modules-http.html)). Moreover, the GET goes though so th eproblem is not with CORS. – WoJ Feb 09 '16 at 10:07

0 Answers0