I am trying to post a request to a local elasticsearch. I have configured the elasticsearch.yml to include:
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, Authorization"
I have also tried:
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: X-Requested-With, Content-Type, Content-Length, Authorization
When posting a from Postman there are no issues, but when posting from AngularJS I recieve this error:
XMLHttpRequest cannot load http://localhost:9200/_search/test_index/_search?size=50. Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
The angular code is the following:
$http.post( vm.elasticsearchUrl, query );
var query = {
index: 'test_index',
size: 50,
body: {
"query": {
"filtered": {
"query": {
"wildcard": {
"title": "*" + searchTerm + "*"
}
},
"filter": {
"not": {
"filter": {
"terms": {
"id": [ 1, 12 ]
}
}
}
}
}
}
}
};
-------ANSWER---------
I resolved it by setting Content-Type to undefined, this then removes Content-Type from the request headers.
var promise = $http.post( vm.elasticsearchUrl, query, {
headers: {
'Content-Type': undefined
}
} );
I am not sure if this is the correct way of doing it, so if anyone has any critique I would love to hear it.