how to send FormData and request body in angular httpclient

Viewed 2337

I am updating my angular application package from @angular/http to @angular/common/http. In the process my code is breaking due to following reason

In the current version I am able to send both formdata and request body in the below way

this.http.post(URL, formData, {'header': headerObj, 'body': bodyObj})

but in the upgrade I am only able to send formData or body not both

this.httpClient.post(this.hostname + apiURL, formData, options)

I don't want to convert request body to formData object since I will be sending null or undefined to server which gets converted to string null which I don't want.

Is there a solution to fix this?

1 Answers

Here is Angular docs for httpClient POST: https://angular.io/api/common/http/HttpClient#post.

You can use one of 15 overloads to POSt the data.


As for the form-data and the body, possible alternative is to merge them into a single object and send as body. Then read at the back-end.

Related