0

Currently I am cloning request to add some parameter like this:

    const modifiedReq = req.clone(
      { 
        params: new HttpParams().set('MYPARAM', MyParamValue),
      })

Now, to the same request I want to add this in header section:

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

How can I add this?

Update:

I am aware of setting header just like this example:

request = request.clone({
setHeaders: 
{
    Authorization: `Bearer ${token}`
}
});        

This way 'Content-Type' etc can be set. But in my example above, there is something meta is set inside which there are paremeters http-equiv and content which I don't know how to set.

(Additional info: This I am adding in header to allow to mix secure & non-secure requests)

5
  • Does this answer your question? How I add Headers to http.get or http.post in Typescript and angular 2? Commented Sep 6, 2022 at 7:32
  • @karoluS Not exactly because how to add header sections 'Content-Type' etc described there however in this case meta data is added inside which there are sections like http-equiv etc are there this is bit confusing to me. Commented Sep 6, 2022 at 7:36
  • If I understand you correctly. http-equiv is just an attribute that allow us frontend developers to emulate HTTP headers. If you want to have this sent as part of the request, you do not need to add it to the header. What matters is value of http-equiv and value of content. Here's a bit of documentation that might shed some light: developer.mozilla.org/en-US/docs/Web/HTML/Element/meta Commented Sep 6, 2022 at 7:40
  • @karoluS I am coming from this answer where he tells what to sent to server in header, but juts not sure how can that be sent via req.clone in Angluar Commented Sep 6, 2022 at 7:50
  • 1
    You should just add this piece of code to your index.html. Commented Sep 6, 2022 at 7:54

1 Answer 1

0

The clone method allows you to pass new or modified headers.

Try this code:

import {
  HttpEvent,
  HttpInterceptor,
  HttpHandler,
  HttpRequest,
  HttpHeaders
} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';

import {environment} from '../../../../environments/environment';

export class SetHeaderInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    const headers = new HttpHeaders({
      'Authorization': 'token 123',
      'WEB-API-key': environment.webApiKey,
      'Content-Type': 'application/json'
    });


    const cloneReq = req.clone({headers});

    return next.handle(cloneReq);
  }
}

Piece of code taken from this thread : Interceptor Angular 4.3 - Set multiple headers on the cloned request

Sign up to request clarification or add additional context in comments.

1 Comment

How to set meta and then http-equiv & `content' in header?

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.