Required Multipartfile Parameter 'file' is not present when I make a request through Angular 4

Asked

Viewed 170 times

0

I can make a file upload request through Postman, but when I make the Angular request, WARN is returned "Required Multipartfile Parameter 'file' is not present"

Follow my API feature made in Java with Spring boot.

@PostMapping
public ResponseEntity<Conteudo> publicaConteudo(@RequestParam("file") MultipartFile file) throws JsonParseException, JsonMappingException, IOException 
{
   /* ANYTHING */
   return ResponseEntity.ok(new Conteudo());
}

And my Angular service. I’m using JWT, but I’m also trying to request using Httpclient.

upload(file: File, conteudo: Conteudo): Promise<any> {
  let formData: FormData = new FormData();

  formData.append('file', file, file.name);

  /* USING JWT
  return this.http.post(this.conteudoUrl, formData)
   .toPromise()
   .then(response => response.json());
  */
  let h1 = new HttpHeaders().set('Authorization', 'Bearer ' + 
  localStorage.getItem('token'));
  const req = new HttpRequest('POST',this.conteudoUrl, formData, {
     headers: h1
  });
  return this.httpClient.request(req).toPromise();
}

I can make the request by Postman normally.

inserir a descrição da imagem aqui

I tried many solutions I found, like creating a Multipartresolver Bean and etc, but no solution solved my problem and what I find most interesting is that it works normally on Postman.

Header of the request in the browser causing the problem. inserir a descrição da imagem aqui

Header of the Postman where the request works correctly. inserir a descrição da imagem aqui

  • Have you looked at the Networks tab as the request is going ?

  • @Andréroggericampos I looked at it.. And I compared it to Postman’s. I couldn’t visualize anything that might be causing this problem. I’ll print it out and put it in the description.

  • @Andréroggericampos I put the headers in the description

  • Did you manage to solve? I have the same problem.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.