XMLHttpRequest is not working

I am new to the elasticsearch and JavaScript development. I am trying to use the code below, so I can do regular REST calls and get JSON back.

Here is my code that I am using in section

function myFunction() {

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://d77511xxx.us-central1.gcp.cloud.es.io:9243', true);
xhr.withCredentials = true;
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'Basic ' + btoa('user:password'));
xhr.send('{"query":{"match_all":{}}}');
}

Here is my additional setting that I am using in elasticsearch.yml

http.cors.enabled: true
http.cors.allow-origin: "/.*/"
http.cors.allow-credentials: true
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, Authorization"

But for some reason I am getting 401 or other issues.

Here is my ElasticSearch Server Link: http://d77511xxx.us-central1.gcp.cloud.es.io:9243

1 Like

Hi @danreut89

Note that I removed some chars from your cluster id (we recommend only including the first 6). Note also that we offer direct support for cloud - login and visit https://cloud.elastic.co/help

Very quickly though the first thing I noticed is that you have http but 9243 is an https port. So try https:// instead.

401 is normally an bad user/password combo. Are you using the elastic and the password generated when you created the cluster (or when you reset it from the security tab)

eg Does the same call work from a curl, if you do

curl -vv -u user:password 'https://d77511xxx.us-central1.gcp.cloud.es.io:9243' -d '{ "query": { "match_all": {} }' -H 'Content-Type: application/json'

(with the correct user+password and the full cluster id)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.