2

So I want to send a HttpClient get Request From Angular with Bearer Token. The API Call works fine with Postman using Bearer token but for some reason it returns error code 500 when sent from Angular. This is my service code :

getCurrentUser(): Observable<any> {
let token = localStorage.token
console.log(token)
let httpHeaders = new HttpHeaders().set('Authorization', "Bearer " + token);
console.log(httpHeaders)
return this.http.get(`${this.baseUrl}/user/getCurrentUser`, {headers : httpHeaders});

}

It is not a CORS issue, seeing as I already checked with Allow-CORS Chrome extension, also, this is my Request Filter from server side:

   @Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class RequestFilter implements Filter{
    
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain){
        
        HttpServletRequest request = (HttpServletRequest) req; 
        HttpServletResponse response = (HttpServletResponse) res; 
        
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with, x-auth-token");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Controll-Allow-Credentials", "true");
        
        if(!request.getMethod().equalsIgnoreCase("OPTIONS")){
            
            try{                              
                chain.doFilter(req, res);
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, PUT,GET, DELETE");
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with, x-auth-token, authorization, content-type, access-control-request-headers, access-control-request-method, accept, origin,");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setStatus(HttpServletResponse.SC_OK);
        }
    }
    
    public void init(FilterConfig filterConfig){}

    public void destroy(){}
}

So what is the problem here ?

Savan Padaliya
  • 828
  • 1
  • 11
  • 22
Clover
  • 21
  • 3
  • What is the error you are getting? What language/framework are you running on the server? – cjd82187 Jul 20 '20 at 13:31
  • Try it by keeping "A" to lowercase of Authorization. it worked for me. – Savan Padaliya Jul 20 '20 at 13:52
  • @cjd82187 this is the error I am getting : Http failure response for http://localhost:8888/user/getCurrentUser: 500 OK I'm running spring boot for the server – Clover Jul 20 '20 at 13:59
  • @SavanPadaliya Already tried that, it doesn't work. – Clover Jul 20 '20 at 14:00
  • why guess ? compare your postman request with the request on the chrome devtools/network tab. if it's the same request, it will produce the same response. – Stavm Jul 20 '20 at 14:26
  • @Stavm Postman will not match what chrome does, as Postman is not a browser and will not have CORS related issues. But, it is a good test to check. If it works in Postman but not in your browser its extremely likely a CORS Issue – cjd82187 Jul 20 '20 at 14:46
  • @Clover you should debug your server and see where the error is coming from on the server if it doesn't send that info to the client. I don't know anything about "Allow-CORS Chrome extension", but if your request works in Postman but not Chrome and the request and headers are the same, its probably a CORS issue. – cjd82187 Jul 20 '20 at 14:47
  • I think that error code 500 is "Internal Server Error", indicates that the server encountered an unexpected condition that prevented it from fulfilling the request, it's not about Authorization – Eliseo Jul 20 '20 at 17:10

0 Answers0