I have done it globally. But this is not 100%. I could not find a method in which angular 4 calls JSON.stringify on the body. I hope someone can help here. Until the new HttpClient appears in version 4.3, I continue to use the wrapper class for the Http service. I do this because interceptors are not present in Angular2 and forward. My cover looks something like this.
@Injectable() export class MyDao { constructor(private http: Http) { } public get(options?:MyRequestOptions){...} public post(url:string,body,options?:MyRequestOptions){ let reqOptions = new BaseRequestOptions(); reqOptions.url = url; reqOptions.params= this.processParams(options); reqOptions.header= this.processHeaders(options);
So far so good. But I did not find any good transformRequest, as in AngularJS, so until I find it, I implemented transformEmptyStringAsNull like this:
private removeEmptyStringsInBody(body:any) { let bodyTemp= JSON.stringify(body, function (key, value) { return value === "" ? null : value }); return JSON.parse(bodyTemp); }
I know this is ugly in the sense that I will again make an extra line back for parsing. But I do not need to do anything in the rest of the application.
Jens alenius
source share