moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
292 stars 118 forks source link

Multipart and body parsers #308

Open furanzujin opened 2 years ago

furanzujin commented 2 years ago

Hello @icebob,

In the File upload aliases section of the documentation, a note indicates that:

Please note, you have to disable other body parsers in order to accept files.

As we need to use both multipart and the JSON body parser on the same route, we experimented a bit with this use case on an auto-aliased route.

Moleculer web routes settings

{
  routes: [
    {
      path: '/',
      whitelist: buildWhitelist(),
      mappingPolicy: 'restrict',
      authorization: true,
      authentication: true,
      autoAliases: true,
      bodyParsers: {
        json: true,
      },
      aliases,
    },
    ...otherRoutes,
  ],
}

Service definition

{
  name: 'poc',
  actions: {
    multipart: {
      rest: {
        method: 'POST',
        path: 'multipart/:id',
        type: 'multipart',
        authentication: false,
        authorization: false,
      },
      handler: multipartHandler,
    },
    stream: {
      rest: {
        method: 'POST',
        path: 'stream/:id',
        type: 'stream',
        authentication: false,
        authorization: false,
      },
      handler: streamHandler,
    },
    standard: {
      rest: {
        method: 'POST',
        path: 'standard/:id',
        authentication: false,
        authorization: false,
      },
      handler: standardHandler,
    },
  },
}

Using the above configurations, it appeared that the three types of routes (standard, multipart, and stream) work together on the same route despite the documentation note.

It is quite good news for us but is this behavior expected to last in further versions? In this case, shall we create a pull request to fix the documentation?

NOTE: also the documentation (Multipart parameters) indicates the ctx.params.$params contains parameters from URL querystring, though it appears they are in ctx.meta.$params instead.

icebob commented 2 years ago

Hmm, it looks parser's conflict is solved by time.

Yes, please create a PR with the fixes. Thanks in advance!

furanzujin commented 2 years ago

As good as done.

Though we made a few more tests to double-check the behavior. It appears that we cannot receive the content-disposition headers provided in each multipart file section. Is there any way to retrieve this information?

icebob commented 2 years ago

I have no idea. You can check the busboy source code.