Ionic 2 app does't send content-type application/json for all post request


#1

Hi all,
i have a problem with http post request; it seem that there is an issue into framewrok because the same request with angular 2 pure application work fine. I use two different version of code but both do not work. Here my code:

Search(siteId: string, docId: string, folderId: string, page: number, searchText: string): any {
//let body = new FormData();
//body.append('text', searchText);
var body: any = { text: searchText };
let uri = EndpointEnums.Search + '/' + (siteId || 'All') + '/' + (docId || 'All') + '/' + (folderId || 'All') + '/' + (page || 1);
return this.rest(uri, "application/json", body , RequestMethod.Post);}

rest(uri: string, contentType: string, body: any, verb: RequestMethod): Observable<any> {
var headers = new Headers();
headers.append('Content-Type', contentType)
headers.append('Accept', 'application/json');

let opts = {
  url: this.environment + uri,
  body: body,
  method: verb,
  headers: headers,
  responseType: ResponseContentType.Json,
  withCredentials: true
};
let requestOptions = new RequestOptions(opts);
return this.http.request(new Request(requestOptions)).map(this.extractData).catch((error) => {
  return this.handleError(error);
});

}

I use another version of previous code but i haven’t resolve:

 let headers = new Headers({ 'Content-Type': 'application/json' });
   let options = new RequestOptions({ headers: headers });

   return this.http.post( this.environment + uri, body, options)

If i use as content-type “application/json” it is not passed correctly the parameters into the header of the request; i use fiddler to intercept the request:

If I change content-type with “application/x-www-form-urlencoded” or “text/plain” the request is well formed (but server refuse because is configured for content-type ‘application/json’); here the request intercepted:

Here the response:

With Firefox rest client and another angular 2 (no ionic 2 app) all request work fine. I used ionic 2 beta rc 3 and this morning i upgrade to latest release, but I havent resolver. Here my package.json:

Why with content-type “application/json” the header is not well formed?

Any ideas?

Thanks,

Fabio


#2

I have the same problem. Can anyone help?


#3

OP’s code is way overengineered. Pass an object as the body to post() and the content-type will be set for you.