3

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

Omkar Jadhav
  • 656
  • 1
  • 5
  • 18

3 Answers3

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