headers issue

return'',{ 'param1' : 'one', 'param2': 'two'}, {'headers': { 'Content-Type': 'x-www-form-urlencoded' }}).map(res => res.json());

error :

Argument of type ‘{ ‘headers’: { ‘Content-Type’: string; }; }’ is not assignable to parameter of type ‘RequestOptionsArgs’. Types of property ‘headers’ are incompatible. Type ‘{ ‘Content-Type’: string; }’ is not assignable to type ‘Headers’. Object literal may only specify known properties, and ‘‘Content-Type’’ does not exist in type ‘Headers’.

I’ve import
import { Http, Headers } from '@angular/http';

It might be that it has to be all lower case, anyway you should use Headers which you imported: new Headers({ "Content-Type": "abc" })

To make it easier, most of the time I create a Headers object with the build in functionality like this:

_getHeaders() {
    let headers = new Headers();    
    headers.append('Content-Type', 'x-www-form-urlencoded');
    return headers;

then your call get’s cleaned up as well:

  get() {
    let url = '';
    let headers = this._getHeaders();
    let body = { 'param1' : 'one', 'param2': 'two'};
    return, body, headers).map(res => res.json());

Off course you could easily make these even more dynamic, but I think you’ll get my point.

Regarding your question: I’m pretty sure you don’t need to set your content to x-www-form-urlencoded and that your body isn’t compliant with that content-type as well. You could just do this to avoid setting the content-type and getting sense off the correct body for your post:

  let data = new URLSearchParams();
  data.append('username', username);
  data.append('password', password);
  let body = data.toString()

this way (when you correctly set the body) angular can determine which content type it should use by itself.

Some extra info about the subject can be found in this stackoverflow post:

1 Like

thanks a lot it’s working with new Headers({ "Content-Type": "abc" })

thanks it’s working with Headers, thanks for explaining. :grinning: