4

I have a Angular (version 8) application which is going to redirect / call an external url (not API) with HEADERS.

I am able to call / redirect the external url but not able to send any HEADERS.

The thing is once I am able to LOGIN successfully, I have to send the Authentication token in header to the external url while redirecting.

Please let me know how can I send auth token while redirecting to external url.

I have gone through the below url which helps me to redirect to external url but headers are not getting sent.

Angular 6 Redirect to external url using POST

2 Answers 2

1

I think the way you'd do it is to send the headers through an XHR request. Then if you wanted to reload afterwards you could. But the header transmission would happen first

this.http
.post('api/login', body, {
  headers: new HttpHeaders({
    'Authorization': 'my-auth-token',
    'x-header': 'x-value'
  })
}).subscribe(() => window.location.href='https://some-other-website.com')
Sign up to request clarification or add additional context in comments.

8 Comments

I tried with this but the post call is getting failed. Status Code: 404 Not Found.. Also tried with GET but same error.. the url which I am accessing is not API url, it is just normal url (api gateway) which redirects to another web page with the auth token which I am getting after Login.
I tried with this and it is working but the headers are not getting sent... const myform = document.createElement('form'); myform.method = 'GET'; myform.action = url; myform.style.display = 'none'; myform.append('tokeninfo', localStorage.getItem('token')); document.body.appendChild(myform); myform.submit();
None of that code made sense to me, sorry. on form submit, send the http request above to the URL of your api with the headers (whatever they need to be) formatted the way your API requires them. All that form stuff you put in your comment is irrelevant to the question
Sorry. I meant to say, I tried with the suggestions you gave first, but getting 404 Error. That's why I tried another way mentioned in stackoverflow.com/questions/53472103/… That code snippets I posted in the comments..Sorry for misleading you...
A 404 error means you are sending to the wrong URL. The URL needs to be changed
|
0
  1. You create the headers.

e.g: JSW, if you have your token in localStorage, as "current_user" token:

import { HttpHeaders } from '@angular/common/http';
....
const headers = new HttpHeaders()
  .append("Authorization", "Bearer " + localStorage.getItem("current_user")["token"])
  .append("Content-type", "application/json");
  1. You define your httpOptions.

    const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', Authorization: 'your-auth-token' }) };

or more tidy (if you already have declared the const headers before

const httpOptions = {
  headers
};
  1. You make your redirect / call to the external url.
    const externalUrl = 'http://....';
    const dataToPut = 'Usually, it will be an object, not a string';
    
    this.http.post<Hero>(this.externalUrl, dataToPut , httpOptions)
        .pipe(
          catchError(this.handleError('post error: ', dataToPut ))
        );

Anyway, once you know how to do it, the best way to send the Authentication token in header is throught an INTERCEPTOR, that is going to intercept all your http calls and insert in the headers the Authentication parameters: https://angular.io/guide/http#intercepting-requests-and-responses

P.S: I strongly recommend you to read this 2 official documentation pages for further examination... is all there, a more detailed information

https://angular.io/guide/http

https://angular.io/guide/http#adding-headers

[Angular] [http] [headers] [interceptor]

1 Comment

Using an Interceptor, how would you filter out some calls that don't need to have a auth header set?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.