middyjs / middy

🛵 The stylish Node.js middleware engine for AWS Lambda 🛵
https://middy.js.org
MIT License
3.73k stars 376 forks source link

httpMultipartBodyParser only accepts 'content-type' in lowercase #474

Closed thanh-taro closed 3 years ago

thanh-taro commented 4 years ago

@middy/core version 1.0.0-beta3 @middy/middy/http-multipart-body-parser version 1.0.0-beta3

I have a problem with httpMultipartBodyParser. It seems only accept content-type in lowercase.

This works:

{
    "content-type": "multipart/form-data"
}

This doesn't work:

{
    "Content-Type": "multipart/form-data"
}

and throws Error:

Error: Missing Content-Type
    at new Busboy (/app/node_modules/busboy/lib/main.js:23:11)
    at Busboy (/app/node_modules/busboy/lib/main.js:9:12)
    at parseMultipartData (/app/node_modules/middy/src/middlewares/httpMultipartBodyParser.js:42:14)
    at before (/app/node_modules/middy/src/middlewares/httpMultipartBodyParser.js:27:16)
    at runNext (/app/node_modules/middy/src/middy.js:70:24)
    at before (/app/node_modules/middy/src/middlewares/jsonBodyParser.js:22:5)
    at runNext (/app/node_modules/middy/src/middy.js:70:24)
    at before (/app/node_modules/middy/src/middlewares/httpHeaderNormalizer.js:85:7)
    at runNext (/app/node_modules/middy/src/middy.js:70:24)
    at before (/app/node_modules/middy/src/middlewares/httpEventNormalizer.js:11:12)
    at runNext (/app/node_modules/middy/src/middy.js:70:24)
    at before (/app/node_modules/middy/src/middlewares/warmup.js:20:7)
    at runNext (/app/node_modules/middy/src/middy.js:70:24)
    at runMiddlewares (/app/node_modules/middy/src/middy.js:91:3)
    at instance (/app/node_modules/middy/src/middy.js:163:5)
    at /app/node_modules/serverless-offline/src/ApiGateway.js:941:17
    at new Promise (<anonymous>)
    at handler (/app/node_modules/serverless-offline/src/ApiGateway.js:540:16)
    at module.exports.internals.Manager.execute (/app/node_modules/@hapi/hapi/lib/toolkit.js:41:33)
    at Object.internals.handler (/app/node_modules/@hapi/hapi/lib/handler.js:46:48)
    at exports.execute (/app/node_modules/@hapi/hapi/lib/handler.js:31:36)
    at Request._lifecycle (/app/node_modules/@hapi/hapi/lib/request.js:312:68)
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
    at Request._execute (/app/node_modules/@hapi/hapi/lib/request.js:221:9)

Please, help me to check. Thank you a lot.

willfarrell commented 4 years ago

Do you have const httpHeaderNormalizer = require('@middy/http-header-normalizer') before the middleware?

Here is an end-to-end example:

const middleware = middy(handler)
    .use(httpEventNormalizer())
    .use(httpHeaderNormalizer())
    .use(httpUrlencodePathParametersParser())
    .use(httpUrlencodeBodyParser())
    .use(httpJsonBodyParser())

    .use(httpCors())
    .use(httpSecurityHeaders())
    .use(
      httpContentNegotiation({
        availableLanguages: ['en-CA', 'fr-CA'],
        availableMediaTypes: ['application/vnd.api+json']
      })
    )
    .use(authorization({ accessRole }))
    .use(validator({ inputSchema, outputSchema, ajvOptions }))
    .use(httpErrorHandler())

I hope that helps.

thanh-taro commented 4 years ago

Yes. I know. But what if I use httpHeaderNormalizer with option canonical=true, which will transform the header into Content-Type? I think httpMultipartBodyParser should accept the header without care about lowercase or uppercase, or at least, it should accept both content-type and Content-Type.

willfarrell commented 4 years ago

Can you put a PR together?

thanh-taro commented 4 years ago

Sure.