question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when POST file multipart/form-data

See original GitHub issue

Hi, I got an error every time when trying to POST data to the API. Request:

changeUserAvatar(authParam, file) {
    let formData = new FormData();
    //file is actually new FileReader.readAsDataURL(myId.files[0]);
    formData.append('profile_image', file);
    
    fetch(BASE_URL + 'profile-image', {
      method: 'POST',
      headers: {
	'Content-Type': 'multipart/form-data',
	'Authorization': authParam
      },
      body: formData
    }).then((response) => {
      return response.json();
    }).then((response) => {
      debugger;
    }).catch((error) => {
      console.error(error);
    });
  }

Error: profile_image can not be blank (422).

But it’s not blank!

Request payload: image

What do I do wrong?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

333reactions
dgrahamcommented, Apr 10, 2017

Setting the Content-Type header manually means it’s missing the boundary parameter. Remove that header and allow fetch to generate the full content type. It will look something like this:

Content-Type: multipart/form-data;boundary=----WebKitFormBoundaryyrV7KO0BoCBuDbTL

Fetch knows which content type header to create based on the FormData object passed in as the request body content.

4reactions
maxdonetskcommented, Apr 13, 2017

@dgraham Thanks a lot! You saved a lot of my time 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error when POST file multipart/form-data (JavaScript)
Solved at GutHub: https://github.com/github/fetch/issues/505. I just had to leave Header without pointing any Content-Type manually.
Read more >
Error while upload a file with API using multipart form data
Hi Michael, We've seen this scenario (a 500 error when uploading multi-part data) in the past with customers integrating Blue Prism with Appian....
Read more >
[Multipart/form-data] Getting a wrong media file typer error on ...
I have a REST service trying to send some text fields and a pdf to a third party for signing (Stiply). I keep...
Read more >
[FIX]XMLHttpRequest error status 500 on POST multipart form ...
Please take 5' to look at this bug. Application using binary file Upload may not work with FF3, because the boundary field is...
Read more >
"413 request entity too large" Errors
If you are the author of the script that generates the POST request, and it's submitting a file (or generating data similar in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found