Simon
Simon

Reputation: 2538

typescript error - Types of property 'headers' are incompatible

I'm having a problem with my Angular application. I get a typescript error when I try to do an http call.

This is the error output:

[09:36:28] typescript: C:/xampp/htdocs/project x/anonymous-social/src/pages/home/home.ts, line: 184 Argument of type '{ headers: HttpHeaders; }' is not assignable to parameter of type 'RequestOptionsArgs'. Types of property 'headers' are incompatible. Type 'HttpHeaders' is not assignable to type 'Headers'. Property 'forEach' is missing in type 'HttpHeaders'.

L183: this.http.post(url, JSON.stringify(payload),

L184: {headers: new HttpHeaders({'Content-Type': 'application/json; charset=utf-8'})})

L185: .map((res:Response) => {

I'm not entirely sure what it doesn't like..

This is my actual code:

I'm loading in both http and /common/http

import { HttpHeaders } from "@angular/common/http";
import { Http, Response } from "@angular/http";

and then in the constructor:

private http: Http,

and then finally my js:

getCall(url, payload, text) {
  this.http.post(url, JSON.stringify(payload),
  {headers: new HttpHeaders({'Content-Type': 'application/json; charset=utf-8'})})
  .map((res:Response) => {
    console.log(res)
})

Any ideas what I'm doing wrong? Any help would be appreciated!

Upvotes: 0

Views: 4346

Answers (1)

Fenton
Fenton

Reputation: 250942

This is how I set up the headers, I'm using Headers, not HttpHeaders:

import { Http, Response, Headers, RequestOptions } from "@angular/http";

// ...

const headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8' });
const options = new RequestOptions({ headers: headers });

// ...

Upvotes: 1

Related Questions