0

I am appending the headers in angular service request but the value is not appending correctly, I was created one Interceptor to add header like below

@Injectable()
export class AddHeaderInterceptor implements HttpInterceptor {
  constructor() {}
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {


    // Clone the request to add the new header
    const clonedRequest = req.clone({ headers: req.headers.set('tenantId', 'r 123') });

    // Pass the cloned request instead of the original request to the next handle
    return next.handle(clonedRequest);
  }
}

While i am sending to server the value not appending correctly, it's showin like in below picture, the value 'r 123' is not appending, How do i solve it?

headers image

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Raja Ram
  • 33
  • 4
  • can you confirm first your interceptor is called or not ?? if called then try to remove space here 'r 123' between r and digits and then try – Nirali Apr 01 '19 at 06:53
  • req.clone({ headers: req.headers.set('tenantId', 'r 123') }) is also correct – Tanmay_vijay Apr 01 '19 at 06:58
  • 1
    The request is a [CORS pre-flight OPTIONS request](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests). The server needs to respond with the appropriate [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) headers. – georgeawg Apr 01 '19 at 07:01
  • @georgeawg yes, it;s preflighted request how do i solve this problem – Raja Ram Apr 01 '19 at 07:34

3 Answers3

0

Have you added AddHeaderInterceptor in you app.module.ts.

// ...
providers: [
    // ...
    { provide: HTTP_INTERCEPTORS, useClass: AddHeaderInterceptor, multi: true },
    //...
]
Tanmay_vijay
  • 569
  • 3
  • 12
0

You can try these

const httpOptions = {
  headers: new HttpHeaders({
  'header1':  'test',
  'header2': 'test',
 })
};
req.clone({ headers: httpOptions) });
Bhagwat Tupe
  • 1,905
  • 1
  • 13
  • 28
0

try this for angular 5,this might work,

    const clonedRequest = req.clone({
        setHeaders: { 'tenantId', 'r 123'}  
    });  
Nirali
  • 454
  • 1
  • 6
  • 16