Search code examples
angularhttppostmanhttp-status-code-400

Getting 400 bad request when trying to access an API using Angular, but it works perfectly fine when using PostMan


I have already tested this API and have made a GET request on the API using PostMan and got the response. But when trying to implement it on my Angular 6 application I get a 401 unauthorized error. This is my angular code:

this.http.get('https://api.channeladvisor.com/v1/orders',{headers:{'Authorization':'Bearer *token*','Content-Type':'application/x-www-form-urlencoded','cache-control':'no-cache'}})
.subscribe((response)=>{
  this.channel=response;
    console.log(this.channel)
})
}

I have the token and have not displayed it in the code for security reasons. I have a feeling it has something with the headers syntax but I'm not so sure.


Solution

  • May you can try add them to your headers:

    'authorization': 'bearer ' + token,
    'Authenticationtoken': 'bearer ' + token,
    'X-Requested-With'  : 'XMLHttpRequest'
    

    My API used CakePHP3, same issue happen long ago, after added above 3 parameters everything ok :)

    And more, please track your request by developer tools, and compare the difference between sucess and failed.