Support Center

Name is required.
Email address is required.
Invalid email address
Answer is required.
Exceeding max length of 5KB

How to connect from a pure JS app using Axios

Justin Tilson May 28, 2018 05:08AM +03

I'm trying to get my JavaScript (vue) app to connect to searchly using Axios.

When I try my assigned URL + endpoint with an Authorization header in Postman it works fine.

When I try the exact same call in pure client side JS, using Axios:

url = 'https://thorin-us-east-1.searchly.com/project/project/_search'
config = {
headers: {
'Authorization': 'Basic ' + btoa('my-access-key-name:my-access-key')
}
}

try {
await axios.post(url, config)
} catch (err) {
console.log(JSON.stringify(err))
}

I get the following error:

{
"config":{
"transformRequest":{
},
"transformResponse":{
},
"timeout":0,
"xsrfCookieName":"XSRF-TOKEN",
"xsrfHeaderName":"X-XSRF-TOKEN",
"maxContentLength":-1,
"headers":{
"Accept":"application/json, text/plain, */*",
"Content-Type":"application/json;charset=utf-8"
},
"method":"post",
"url":"https://thorin-us-east-1.searchly.com/project/project/_search",
"data":"{\"headers\":{\"Authorization\":\"Basic my-base64-encoded-access-key-name-and-key\"}}"
},
"request":{
},
"response":{
"data":{
"status":"error",
"message":"Missing authorization header."
},
"status":401,
"statusText":"",
"headers":{
"content-type":"application/json; charset=UTF-8"
},
"config":{
"transformRequest":{
},
"transformResponse":{
},
"timeout":0,
"xsrfCookieName":"XSRF-TOKEN",
"xsrfHeaderName":"X-XSRF-TOKEN",
"maxContentLength":-1,
"headers":{
"Accept":"application/json, text/plain, */*",
"Content-Type":"application/json;charset=utf-8"
},
"method":"post",
"url":"https://thorin-us-east-1.searchly.com/project/project/_search",
"data":"{\"headers\":{\"Authorization\":\"Basic my-base64-encoded-access-key-name-and-key\"}}"
},
"request":{
}
}
}

The authorization header is definitely there so the error message seems incorrect to me.

This sort of inconsistency between browser and server side JS is usually a CORS issue. This post seems to indicate CORS should work fine: http://support.searchly.com/customer/portal/questions/3275944-cors

I'm out of things to try. Anyone have this working?

The access key docs are a full of incorrect examples of how to connect with API keys (not a restricted read-only Access Key): http://docs.searchly.com/access-keys/

1 Community Answers

Best Answer
Justin Tilson May 28, 2018 05:28AM +03

Sometimes answers are simple. I changed

await axios.post(url, config)

to

await axios.get(url, config)

and now it works.



Post Your Public Answer

Your name (required)
Your email address (required)
Answer (required)
support@searchly.com
http://assets0.desk.com/
false
searchboxio
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
false
Invalid characters found
/customer/en/portal/articles/autocomplete