Hi,

I'm using Angular 6.
I'm trying to add Content-Type header to my http request, but it doesn't 
work and I have no idea what else I can try.

My first approach was to simply add header in http.post as in official 
documentation https://angular.io/guide/http#adding-headers:

  login(email: string, password: string): Observable<LoginResultModel> {
   const httpOptions = {
     headers: new HttpHeaders({
       'Content-Type': 'application/vnd.api+json'
     })
   };
   return this.http.post<LoginResultModel>(
     `${this.baseUrl}/auth`,
     {
       username: email,
       password: password
     },
     httpOptions
   );
 }
}

And the second was to use interceptor:

import { Injectable } from '@angular/core';
import {
 HttpInterceptor, HttpHandler, HttpRequest
} from '@angular/common/http';

@Injectable()
export class HeaderHttpInterceptor implements HttpInterceptor {
 intercept(req: HttpRequest<any>, next: HttpHandler) {

    const modifiedReq = req.clone({
     setHeaders: {
       'Content-Type': 'application/vnd.api+json'
     }
   });

    return next.handle(modifiedReq);
 }
}

But none of them works, my header is still like this:

   1. Content-Type: 
   text/html; charset=UTF-8
   
   

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to angular+unsubscr...@googlegroups.com.
To post to this group, send email to angular@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to