0

Need to remove Authorization Bearer in POST Call

Below code didnt remove the authorization bearer which is coming from http interceptors.

Status code: 403 Unauthorized RequestAuthorization header is present, this is not supported

public EMail(Request) {
 const headers = new HttpHeaders()
                .delete('Authorization', 'Bearer ' + sessionStorage.removeItem('accessToken'));
            return this.http.post<Email>(sendMailUrl, request, { headers }).map((data: any) => {
});

2 Answers 2

1

If the authorization header gets added in the HTTP intercepter, then you can not 'remove' the token like this. The HTTP intercepter needs to be adjusted.

Go to your intercepter file, which should start something like this:

public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

You can then not add the Authorization header based on the url being called:

if (req.url !== 'your_email_post_url') {
   // add authorization header code
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your code is executed before the http interceptor. You should enhance your interceptor to only append the header for the desired server host.

If you use a lib you can usually define the url.

E.g. https://github.com/manfredsteyer/angular-oauth2-oidc

OAuthModule.forRoot({
    resourceServer: {
        allowedUrls: ['http://www.angular.at/api'],
        sendAccessToken: true
    }
})

1 Comment

This is the correct solution if the oauth2-oidc library is being used and the emailUrl is indeed contacting a different domain/API

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.