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)

Atul
  • 3,778
  • 5
  • 47
  • 87
  • Does this answer your question? [How I add Headers to http.get or http.post in Typescript and angular 2?](https://stackoverflow.com/questions/42352854/how-i-add-headers-to-http-get-or-http-post-in-typescript-and-angular-2) – karoluS Sep 06 '22 at 07: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. – Atul Sep 06 '22 at 07: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: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta – karoluS Sep 06 '22 at 07:40
  • @karoluS I am coming from [this](https://stackoverflow.com/a/66080221/1911652) 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 – Atul Sep 06 '22 at 07:50
  • 1
    You should just add this piece of code to your index.html. – karoluS Sep 06 '22 at 07:54

1 Answers1

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

JakaRanga
  • 16
  • 2