简体   繁体   中英

how to send FormData and request body in angular httpclient

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM