Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 - Can't set header for HttpClient

I am trying to make a POST call using HttpClient in an Angular 5 project, and I want to set the header:

import { HttpClient, HttpHeaders, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';

import { AuthData }    from './models/auth-data';

@Injectable()
export class AuthService {

    constructor(private http: HttpClient) { }

    auth = (data: AuthData) => {

        var url = "https://.../login";
        var payload = data;
        var headers = new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8');
        var options =  {
            headers: headers
        };

        this.http.post(url, payload, options).subscribe();
    }
}

For some reason, the Content-Type header does not seem to be in the request I am making.

enter image description here

Why is this?

like image 397
Enrique Moreno Tent Avatar asked Nov 17 '17 15:11

Enrique Moreno Tent


1 Answers

This worked for me. Instead of append.

let headers = new HttpHeaders({
'Content-Type': 'application/json'
});
like image 102
Ronald Dsouza Avatar answered Sep 22 '22 06:09

Ronald Dsouza