4

I am trying to use qwest to send some data to Elasticsearch:

qwest.post(
    'http://elk.example.com:9200/incidents',
    this.incident,
    {cache: true}
)
    .then(function (xhr, response) {
        console.log('incident posted')
    })
    .catch(function (e, xhr, response) {
        console.log('error posing incident: ' + e)
    })

where this.incident is an Object (from Vue.js).

The call fails with a 406 (Not Acceptable) error, which I understand being an information from the Elasticsearch server telling me that I wanted an answer in some format, which he cannot use.

The call fails (there is no document indexed) nevertheless, so I am not sure if my understanding is correct?

If so - what is a correct format to ask for?

WoJ
  • 27,165
  • 48
  • 180
  • 345
  • Do you want to create a new document and also create the index at the same time? – Val May 31 '17 at 11:51
  • @Val: yes, for the first call. But then also use the same call to index a new document (when the index has been created by the first call). I can create the index manually if this helps (I thought it did not matter) – WoJ May 31 '17 at 11:53
  • @Val: I just tried to create the index first (`PUT /incidents`), the index was created and reissuing the call in my question leads to the same issue (so it does not matter whether the index is present or not) – WoJ May 31 '17 at 11:55
  • 1
    note that you're using `post` and not `put`. Also the URL should have a document type, i.e. `http://elk.example.com:9200/incidents/incident`. It will probably work after that – Val May 31 '17 at 12:04
  • @Val: you are right, I forgot to add the document type in my request. This did not solve the issue, though. – WoJ May 31 '17 at 12:14
  • Do you get the same error? – Val May 31 '17 at 12:15
  • Yes, still the 406 – WoJ May 31 '17 at 12:16
  • How does the error look like in your ES server log? – Val May 31 '17 at 12:19
  • There are no errors in the log. I tried with a plain jQuery AJAX call, same thing (so this is not an issue with the library). I then tried to GET the status (call to a bare `http://example.com:9200`) and I get a result (via Javascript). So this is really the `POST` part which fails (you mentioned `PUT` but a `POST` of some data with `curl` was successful) – WoJ May 31 '17 at 12:51
  • Are you sure that your `incident` object gets serialized as JSON ? – Val May 31 '17 at 13:11
  • @Val: that was it, I was passing an Object (dictionary) which was then encoded and (I assume) sent with the default encode to ES. Would you mind just bootstrap an answer, I will then add code that works (serialized JSON and `application/json` type) - you had the right idea so the glory and delude of rep is yours :) – WoJ May 31 '17 at 17:56

1 Answers1

10

The incident object is not a properly serialized JSON string. You need to call JSON.stringify(this.incident) in order to get the equivalent JSON string, and specify the application/json HTTP header.

$.ajax({
            url: 'http://example.com:9200/incidents/incidents',
            type: 'POST',
            data: JSON.stringify(this.incident),
            dataType: 'json'
        })
WoJ
  • 27,165
  • 48
  • 180
  • 345
Val
  • 207,596
  • 13
  • 358
  • 360