12

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?

8
  • Is you request even firing? Have you checked with logs or debug if your headers get set before the request fires? Anyways you should be supposed to return an Observable not a Subscription. Commented Nov 17, 2017 at 16:10
  • 1
    Content-Type doesn't change, what does change mean, change from what to what? :) Commented Nov 17, 2017 at 16:50
  • can you specify, what exactly does it mean that 'header does not seem to be in request'? says who? devtools in browser? or server endpoint? or? Commented Nov 17, 2017 at 20:06
  • Yes, the developer tools. I have updated the question with a sample. Commented Nov 17, 2017 at 20:11
  • 1
    there should be 2 'requests in question'. http method for one should be OPTIONS (the one you showed here, its called preflight cors request) and one actual POST (if server allows it for your client). you might benefit from reading about cors. if host is different than locahost:4200, which i assume is, then you have to enable cors requests on your server for localhost:4200 client. Commented Nov 17, 2017 at 21:02

6 Answers 6

9

This worked for me. Instead of append.

let headers = new HttpHeaders({
'Content-Type': 'application/json'
});
Sign up to request clarification or add additional context in comments.

Comments

6

because HttpHeaders is immutable we have to assign it

const _headers = new HttpHeaders();
const headers = _headers.append('Content-Type', 'application/json')
                        .append('...', '...')
                        .append('...', '...');

1 Comment

this is the best answer
5

If i see correctly, what you're showing us is OPTIONS preflight request (CORS), not actual POST request.

There should be 2 'requests in question'. http method for one should be OPTIONS (the one you showed here, its called preflight cors request) and one actual POST (if server allows it for your client). If host is different than locahost:4200, which i assume is, then you have to enable cors requests on your server for localhost:4200 client.

1 Comment

What if it is set for 'Access-Control-Allow-Origin' : '*', but still gets preflight request ?
-1

Try this:

var headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');

2 Comments

One reason this won't work is that headers is immutable. You need to assign the result of the append method (which makes a clone of the headers) back to the headers: headers = headers.append(blah)
headers = headers.append('Content-Type', 'application/json');
-1

Try this too I had the same issue ;)

ng build --production -host=yourDomain.com

The thing is that the project is built on localhost, using node, and keep these default host and port info, you can change it when you are building your project

Comments

-3
const httpOptions = {
        headers: new HttpHeaders({
          'Content-Type':  'application/json',
          'id':id
        })
      };          

note: please send the 'id' as 'String', observe the below modification

 const httpOptions = {
        headers: new HttpHeaders({
          'Content-Type':  'application/json',
          'id':id+''
        })
      }; 

it's working fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.