0

I am sending a form data using HTTP post where it will be like this

     saveDataFile(mutlidata,id,value): Observable<Response> {

        var _url = 'http://xxxx.xxx.xxx';
        var saveDataURL = _url + '/' + id;
        var _this = this;

        let headers = new Headers();
        headers.append('Content-Type', 'multipart/form-data');
        let options = new RequestOptions({ headers: headers });
        const frmData = new FormData();
        frmData.append("file",JSON.stringify(mutlidata),'sample.json');
        frmData.append("dataCheck",value);


        return this.http.post(saveDataURL,frmData, options).pipe(
            map((res: Response) => {
                 return res;
            }),
            catchError(this.handleError),);


    }

here multidata will have

    [["fakepath/test1.JPG","ea305e-be9d"],["fakepath/test2.JPG","489ce580-c50e-40e6-b1ab-71c7827f636c"]]

    id will have 15asdas6asd6

    value will have  either true / false

here it is not sending the form data I guess and when I debug and checking the frmdata in return this.http.post(saveDataURL,frmData, options) here it is display form.entries, values, etc

in the sample.json file I'm appending the formdata

frmData.append("file",JSON.stringify(mutlidata),'sample.json');

and here file means I will have to send the data to the API using that key word

IMParasharG
  • 1,869
  • 1
  • 15
  • 26
sai686
  • 21
  • 7
  • 1
    Do you subscribe to your observable? Like `saveDataFile(mutlidata,id,value).subscribe()`. If not, the request will never be sent. – Askirkela May 07 '19 at 09:32
  • Possible duplicate of [Angular 2 http.post() is not sending the request](https://stackoverflow.com/questions/36208732/angular-2-http-post-is-not-sending-the-request) – Igor May 07 '19 at 17:19

1 Answers1

0

There is no need to append the Content-Type, as the browser will handle it for you. The last time I tried manually adding it to my post request, it has caused my HTTP request to fail. You can just remove the part of the code.

saveDataFile(mutlidata,id,value): Observable<Response> {

  var _url = 'http://xxxx.xxx.xxx';
  var saveDataURL = _url + '/' + id;
  var _this = this;

  const frmData = new FormData();
  frmData.append("file",JSON.stringify(mutlidata),'sample.json');
  frmData.append("dataCheck",value);

  return this.http.post(saveDataURL, frmData, options).pipe(
    map((res: Response) => {
         return res;
    }),
    catchError(this.handleError),);
}
wentjun
  • 40,384
  • 10
  • 95
  • 107