Structure of input data in a content-type: Angular form-data

1

They are asking me to send some data through the post method. I leave the code first:

addImage(image, params): Observable<any> {


const httpOptions = {
  headers: new HttpHeaders({
    'Authorization': this.token,
    'Content-Type': 'multipart/form-data',
    'Content-Disposition': 'form-data; filename=${image.name}'
  })
};
this.data = params;
this.data.file = image;
return this.http.post(this.url , this.data, httpOptions);

The request is not worth it, I think what they expect to receive is the data of a form (form that neither exists nor will exist). There is some way to simulate how that entry would be. That is, how to simulate the input that the form would give us when launching?.

Greetings.

    
asked by Findelias 23.11.2018 в 15:00
source

1 answer

2

What you have to do is create a FormData and add the image like this:

const fd= new FormData();
fd.append('nombre',image, image.name); //el nombre del fichero es opcional

and add the rest of values of the form of similar way:

fd.append('atributo',data.atributo);
...
const httpOptions = {
  headers: new HttpHeaders({
    'Authorization': this.token,
    'Content-Type': 'multipart/form-data'
  });
};
this.http.post(url,fd,options);
    
answered by 23.11.2018 / 15:38
source