0

I am using Postman with these headers.

enter image description here

If I keep the default Content-Type, it will throw me the error

Error: Malformed part header

However, if I remove the default Content-Type and put in my own (see the last key), it will NOT be able to scan my uploaded file (req.file is undefined). If I have any sort of boundary, the same error is thrown.

Thus, I am leaning towards thinking that the boundary is the problem. However, if I remove it, I won't be able to see my req.file

Here is my node.js code

const multer = require("multer")

const upload = multer({
    storage: multer.diskStorage({
        destination: (req, file, callback) => {
            callback(null, "./images")
        },
        filename: (req, file, callback) => {
            callback(null, file.originalname)
        }
    })
})

app.post("/single", upload.single("upload"), (req, res) => {
    console.log(req.file);
    res.send("Testing123")
})

UPDATE: I have not found the answer yet, but when i closed and reopened postman, the Error: Malformed part header No longer shows. However, my req.file is still undefined

and because someone asked if another stack overflow question answered my question: nope, it didn't, and here is why. I am already doing Answer 1 Answer 1 , the checked answer

Answer 3 (multipart/mixed) also makes my req.file undefined

I watched the video from Answer 4 but it's the same as answer one

wel
  • 234
  • 2
  • 11
  • Does this answer your question? [POSTMAN for Multipart/form-data](https://stackoverflow.com/questions/44182746/postman-for-multipart-form-data) – Onboardmass Jun 18 '22 at 02:03
  • 1
    @Onboardmass nope. the `req.file` is still `undefined` . That post was one of the first I stumbled upon actually – wel Jun 18 '22 at 02:23

6 Answers6

1

The answer was pretty stupid. For some reason, it doesn't show req.file, BUT it still processes all the same. It really confused me, but I changed my code to the official multer error handling way, https://www.npmjs.com/package/multer#error-handling

app.post('/profile', function (req, res) {
  upload(req, res, function (err) {
    if (err instanceof multer.MulterError) {
      // A Multer error occurred when uploading.
    } else if (err) {
      // An unknown error occurred when uploading.
    } else {
    // Everything went fine.
      console.log(req.file)
    }

  })
})

i would be able to see it under //Everything went fine portion. It's a bit weird, so if anyone knows the reason why, please tell me

wel
  • 234
  • 2
  • 11
0

I had this issue with Postman as well, i switched to Insomnia and my code worked just fine. The problem appears to be postman and not multer.

Clown
  • 1
  • 1
  • 2
  • Since this doesn't explain much, it might be more appropriate as a comment than answer – PKiong Jul 28 '22 at 03:21
  • Since this doesn't explain much, it might be more appropriate as a comment than answer – PKiong Jul 28 '22 at 03:22
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32335264) – Melaku Minas Kasaye Aug 01 '22 at 07:00
  • What more do you need? CCTV footage?? – Clown Aug 02 '22 at 08:15
0

Malformed part header This error occurs in the postman. This is a postman glitch.(By this method I solved this problem so I am sharing) first of all you have to check the headers properly if all well. 1) Then delete the request you have made in the postman collection. 2) Then make or add a new request and hit the request it should work.

  • 1
    Hi, take a look [here](https://stackoverflow.com/help/how-to-answer). Posting well formatted and focused answers is a strict requirement on Stackoverflow – pierpy Dec 28 '22 at 11:43
0

Yes I confirm that it's a glitch from postman, the solution for me is just deleting the request in my collection and re-configured it and it worked fine

0

Check form code carefully and header:

const sinupsubmit = (e) => {
  e.preventDefault();
  const myform = new FormData();
  myform.set("name",name);
  myform.set("email", email);
  myform.set("password", password);
  myform.set("avatar", avatar)
  dispatch(register(myform))
}

dispatch({type:USER_REQUEST});
const config = {
  headers: {
    "Content-type": "multipart/form-data",
  },
};
const { data } = await axios.post('/user/register', userdata, config);
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

Remove your "enter" button

Im my case, I just press here NOT needed "enter" button. Just remove it from here.

Spawnet
  • 53
  • 6