nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.89k stars 7.56k forks source link

Multipart: Boundary not found #4818

Closed volovodenko closed 4 years ago

volovodenko commented 4 years ago

Bug Report

Current behavior

I have an exception in NestJS as described here https://stackoverflow.com/questions/49692745/express-using-multer-error-multipart-boundary-not-found-request-sent-by-pos

Front-end has received 500 status code.

err: {
    "message": "Multipart: Boundary not found",
    "stack":
        Error: Multipart: Boundary not found
            at new Multipart (/var/www/impactoria/Work/node_modules/busboy/lib/types/multipart.js:58:11)
            at Multipart (/var/www/impactoria/Work/node_modules/busboy/lib/types/multipart.js:26:12)
            at Busboy.parseHeaders (/var/www/impactoria/Work/node_modules/busboy/lib/main.js:71:22)
            at new Busboy (/var/www/impactoria/Work/node_modules/busboy/lib/main.js:22:10)
            at multerMiddleware (/var/www/impactoria/Work/node_modules/multer/lib/make-middleware.js:33:16)
            at /var/www/impactoria/Work/node_modules/@nestjs/platform-express/multer/interceptors/file.interceptor.js:15:81
            at new Promise ()
            at MixinInterceptor.intercept (/var/www/impactoria/Work/node_modules/@nestjs/platform-express/multer/interceptors/file.interceptor.js:15:19)
            at /var/www/impactoria/Work/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:22:36
            at Object.handle (/var/www/impactoria/Work/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:20:56)
    "context": "ExceptionsHandler"
  }

I can fix it on front-end, but how to avoid this exception on NestJS backend next time? I want that 400 status code was thrown instead 500.

Nest version: 7.0.9

For Tooling issues:

jmcdo29 commented 4 years ago

Please provide a minimum reproduction repository

volovodenko commented 4 years ago

"dependencies": { "@nestjs/common": "^7.0.9", "@nestjs/core": "^7.0.9", "@nestjs/jwt": "^7.0.0", "@nestjs/passport": "^7.0.0", "@nestjs/platform-express": "^7.0.9", "@nestjs/schedule": "^0.3.1", "@nestjs/typeorm": "^7.0.0", "@sentry/node": "^5.15.5", "aws-sdk": "^2.674.0", "axios": "^0.19.2", "bcryptjs": "^2.4.3", "body-parser": "^1.19.0", "class-transformer": "^0.2.3", "class-validator": "^0.12.2", "dotenv-safe": "^8.2.0", "express-rate-limit": "^5.1.3", "faker": "^4.1.0", "handlebars": "^4.7.6", "ioredis": "^4.16.3", "module-alias": "^2.2.2", "ms": "^2.1.2", "nestjs-s3": "^1.0.1", "nodemailer": "^6.4.6", "passport": "^0.4.1", "passport-jwt": "^4.0.0", "pg": "^8.1.0", "pino": "^6.2.1", "rate-limit-redis": "^1.7.0", "reflect-metadata": "^0.1.13", "rxjs": "^6.5.5", "source-map-support": "^0.5.19", "typeorm": "^0.2.24", "uuid": "^8.0.0" }, "devDependencies": { "@nestjs/cli": "^7.1.5", "@nestjs/schematics": "^7.0.0", "@types/bcryptjs": "^2.4.2", "@types/body-parser": "^1.19.0", "@types/dotenv-safe": "^8.1.0", "@types/express": "^4.17.6", "@types/express-rate-limit": "^5.0.0", "@types/faker": "^4.1.11", "@types/ioredis": "^4.16.1", "@types/module-alias": "^2.0.0", "@types/ms": "^0.7.31", "@types/node": "^12.12.38", "@types/nodemailer": "^6.4.0", "@types/passport-jwt": "^3.0.3", "@types/pino": "^6.0.1", "@types/rate-limit-redis": "^1.7.1", "@types/source-map-support": "^0.5.1", "@types/uuid": "^7.0.3", "newman": "^5.0.0", "pino-pretty": "^4.0.0", "pm2": "^4.4.0", "prettier": "^2.0.5", "ts-loader": "^7.0.4", "ts-node": "^8.10.1", "tsconfig-paths": "^3.9.0", "tslint": "^6.1.2", "tslint-config-prettier": "^1.18.0", "tslint-config-standard": "^9.0.0", "tslint-sonarts": "^1.9.0", "typeorm-seeding": "^1.6.1", "typescript": "^3.8.3" }, "optionalDependencies": { "husky": "^4.2.1", "lint-staged": "^10.0.7" }

jmcdo29 commented 4 years ago

Please provide a minimum reproduction repository

This means a git repository with minimum setup that can be cloned, have dependencies installed, and started without anything else. Also, a sample payload would be great in this case

volovodenko commented 4 years ago

O...this isn't possible.

volovodenko commented 4 years ago

But I can provide a brand new test repository https://github.com/volovodenko/nestTest

jmcdo29 commented 4 years ago

@volovodenko the JS you provided in the README crashes the browser request. However, from curl, I'm able to make a request to https://raw.githubusercontent.com/rmolinamir/typescript-cheatsheet/master/TypeScript.png, save the file, then send it to the local server with no problem.

Modify the tsconfig.build.json to have include: ["src"] to keep the server from restarting, then the following works fine

curl https://raw.githubusercontent.com/rmolinamir/typescript-cheatsheet/master/TypeScript.png --output image.png
curl http://localhost:3000/hello -F 'photo=@./image.png'

If the browser crashing is part of the problem, then saying so in your readme would be a good idea. Otherwise, I'm still not sure what the problem is.

volovodenko commented 4 years ago

When you run it from curl, curl will add 'boundary' to request image If we get rid of 'boundary' from request Content-Type, we'll get exception Try it in Postman image image

jmcdo29 commented 4 years ago

That's an invalid request then and it is erroring out as it should. This post has more info and better links, but TL:DR; multipart/form-data requires you have a boundary property

volovodenko commented 4 years ago

So let my App falls every time with such request?

jmcdo29 commented 4 years ago

No, fix the request you are sending in. This isn't a problem with Nest, but a problem with the request being sent.

volovodenko commented 4 years ago

image It must be handled by framework.....or for what I use it?

volovodenko commented 4 years ago

I even don't want to hear from front-end developer about any 500 status code from my App

jmcdo29 commented 4 years ago

If you want the 400 instead of 500, use a filter and transform the exception. That's what the enhancer was made for.

volovodenko commented 4 years ago

For what purpose then I use framework, if it can't handle this exception? Exception was thrown inside framework modules. Ok, I understand. Thank's for 'help'.

kamilmysliwiec commented 4 years ago

Thanks @jmcdo29 for your help.

Please, use our Discord channel (support) or StackOverflow for such questions. We are using GitHub to track bugs, feature requests, and potential improvements. This particular issue is not a bug and you can find more discussions on that in the past issues.