I want to Pass parameters to POST method in angular 2 but it is not working whwn I used params:new httpParams().set() but it is not worked .I also tried params:new httpParams().set() and headers then also it is not worked & finally I got the solution with Formdata
Asked
Active
Viewed 1,047 times
3
-
[link](https://stackoverflow.com/questions/44569409/angular-2-http-post-params-and-body?rq=1) have you checked that? – Pawel May 18 '18 at 10:44
-
Yes, I checked that link but it is laso not worked – Omkar Jadhav May 18 '18 at 13:22
-
Below is the right answer of my question – Omkar Jadhav May 18 '18 at 13:23
3 Answers
5
You can use formData to send http params or any kind of attachment.
let formData = new FormData();
formData.append('key1', 'value1');
formData.append('key2', 'value2');
// http request with post method
this.httpService.post('Url', formData);

Vaibhav Gaikwad
- 811
- 2
- 12
- 21
3
Their is way to pass data to post method in angular 2 by using the Formdata,while passing the data their is no need of headers because we are using the formdata here. To understand this concept lets take one example as-
saveNewData(name, countryId) {
this.saveNewDataUrl = 'localhost:7575/app/data';
const fd = new FormData();
fd.append('name', name);
fd.append('countryId', countryId);
return this.http.post(this.saveNewDataUrl, fd);
}

Omkar Jadhav
- 656
- 1
- 5
- 18
0
You can do it like below:
var validFileExtensions: string[] = ['jpg', 'jpeg','png'];
this.uploadedFiles.append('id', this.userInfo.id.toString());
this.uploadedFiles.append('validImageExtensions', validFileExtensions.toString());
this.accountService.uploadImage(this.uploadedFiles).subscribe((result: any) => {
this.alertService.success("Profile settings updated successfully");
},

Abdus Salam Azad
- 5,087
- 46
- 35