I'm running my react app and my curl requests from my local machine. The error shows up in the browser console of the react app, localhost:3000. I used https://curl.trillworks.com/#node to convert my curl request to a javascript request.
This is my request:
var request = require('request');
var options = {
url: 'http://<<ip_address>>//elasticsearch/recipes/_doc/_search',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: {
"size": 1,
"query": {
"match": {
"instructions": query
}
}
},
auth: {
'user': process.env.REACT_APP_ELASTIC_USER,
'pass': process.env.REACT_APP_ELASTIC_PASS
}
};
function callback(error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body);
}
}
request(options, callback);
where 'query' is a string entered by the user. The username and password are entered correctly from the Google Cloud Dashboard. My project is made with the create react app boilerplate and the query is triggered when the user presses enter after selecting a Material UI InputBase search box.
Specifically the errors (in Chrome) are:
-OPTIONS http://ip_address//elasticsearch/recipes/_doc/_search 401 (Unauthorized)
-Access to fetch at 'http://ip_address//elasticsearch/recipes/_doc/_search' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.