Search code examples
javascriptgetaxioshttp-status-code-401

401 error when performing GET request w/ axios


I can view my JSON data in my browser but not in my console. I'm not super familiar with axios, but I think the problem is with the url (see snippet below). That, or I'm missing something within axios.get {.

Any thoughts?

server.js:

axios.get("https://handshake.[redacted].com/HandshakeWebServices/Odata/Odata.ashx/HS[redacted]?hsf=@UserName=%27[redacted]%27&$select=PreferredName,Initials,JobTitle,Office", {
    crossDomain: true,
    method: "GET",
    credentials: "include",
    mode: "no-cors",
    headers: {
        "Accept": "application/json; odata=verbose"
    }
})
.then(function(res){
  console.log(res.data);
})
.catch(function(e){
  console.log(e);
})

console:

GET https://handshake.[redacted].com/HandshakeWebServices/Odata/Odata.ashx/HS[redacted]?hsf=@UserName=[redacted]&$select=PreferredName...etc 401 (Unauthorized)

server.js?1d69:18 Error: Request failed with status code 401
    at createError (createError.js?2d83:16)
    at settle (settle.js?467f:18)
    at XMLHttpRequest.handleLoad (xhr.js?b50d:77)

Solution

  • The site is returning a 401 which means you are not authorised to access the URL.

    Normally, this means you need to send credentials.

    Now, you've said credentials: "include", but that's a property recognised by fetch, not axios. The axios equivalent is withCredentials: true, so set that instead.


    You've also said mode: "no-cors", which is another property recognised by fetch, not axios. You don't want it anyway. Sending credentials requires permission via CORS, so by rejecting CORS, reject the possibility to get permission to send the credentials.

    Remove that property.