How do I process a non-multipart body?

#1

Hi, I ran into following issue {"error":{"message":"unsupported content-type","name":"UnsupportedMediaTypeError" while following documentation on streaming upload.

How do I completely turn off all body parsing (multipart included?)

0 Likes

#2

How did you set the headers of your HTTP request ?

0 Likes

#3
  • Connected to localhost (::1) port 3333 (#0)

POST /v1/server?id=9dabcc41251338a0ad476b3d917e0344 HTTP/1.1
Host: **************
Content-Type: text/plain; charset=x-user-defined-binary
Content-Length: 36

0 Likes

#4

Can you please share the contents of config/bodyparser.js file?

0 Likes

#5
'use strict'

module.exports = {
  /*
  |--------------------------------------------------------------------------
  | JSON Parser
  |--------------------------------------------------------------------------
  |
  | Below settings are applied when the request body contains a JSON payload.
  | If you want body parser to ignore JSON payloads, then simply set `types`
  | to an empty array.
  */
  json: {
    /*
    |--------------------------------------------------------------------------
    | limit
    |--------------------------------------------------------------------------
    |
    | Defines the limit of JSON that can be sent by the client. If payload
    | is over 1mb it will not be processed.
    |
    */
    limit: '1mb',

    /*
    |--------------------------------------------------------------------------
    | strict
    |--------------------------------------------------------------------------
    |
    | When `strict` is set to true, body parser will only parse Arrays and
    | Object. Otherwise everything parseable by `JSON.parse` is parsed.
    |
    */
    strict: true,

    /*
    |--------------------------------------------------------------------------
    | types
    |--------------------------------------------------------------------------
    |
    | Which content types are processed as JSON payloads. You are free to
    | add your own types here, but the request body should be parseable
    | by `JSON.parse` method.
    |
    */
    types: [
      'application/json',
      'application/json-patch+json',
      'application/vnd.api+json',
      'application/csp-report'
    ]
  },

  /*
  |--------------------------------------------------------------------------
  | Raw Parser
  |--------------------------------------------------------------------------
  |
  |
  |
  */
  raw: {
    types: [
      'text/*'
    ]
  },

  /*
  |--------------------------------------------------------------------------
  | Form Parser
  |--------------------------------------------------------------------------
  |
  |
  |
  */
  form: {
    types: [
      'application/x-www-form-urlencoded'
    ]
  },

  /*
  |--------------------------------------------------------------------------
  | Files Parser
  |--------------------------------------------------------------------------
  |
  |
  |
  */
  files: {
    types: [
      'multipart/form-data'
      //      'text/plain; charset = x - user - defined - binary'
    ],

    /*
    |--------------------------------------------------------------------------
    | Max Size
    |--------------------------------------------------------------------------
    |
    | Below value is the max size of all the files uploaded to the server. It
    | is validated even before files have been processed and hard exception
    | is thrown.
    |
    | Consider setting a reasonable value here, otherwise people may upload GB's
    | of files which will keep your server busy.
    |
    | Also this value is considered when `autoProcess` is set to true.
    |
    */
    maxSize: '20mb',

    /*
    |--------------------------------------------------------------------------
    | Auto Process
    |--------------------------------------------------------------------------
    |
    | Whether or not to auto-process files. Since HTTP servers handle files via
    | couple of specific endpoints. It is better to set this value off and
    | manually process the files when required.
    |
    | This value can contain a boolean or an array of route patterns
    | to be autoprocessed.
    */
    autoProcess: true,

    /*
    |--------------------------------------------------------------------------
    | Process Manually
    |--------------------------------------------------------------------------
    |
    | The list of routes that should not process files and instead rely on
    | manual process. This list should only contain routes when autoProcess
    | is to true. Otherwise everything is processed manually.
    |
    */
    processManually: ['/server']

    /*
    |--------------------------------------------------------------------------
    | Temporary file name
    |--------------------------------------------------------------------------
    |
    | Define a function, which should return a string to be used as the
    | tmp file name.
    |
    | If not defined, Bodyparser will use `uuid` as the tmp file name.
    |
    | To be defined as. If you are defining the function, then do make sure
    | to return a value from it.
    |
    | tmpFileName () {
    |   return 'some-unique-value'
    | }
    |
    */
  }
}

0 Likes

#6

Try to add this:

processManually: ['/server', 'server/:id']
0 Likes

#7

Same result. What is needed is a way to disable the multipart body parser. What I did myself as a temporary workaround is this

    /**
     * Body is raw data, parse it and move forward
     */
    if (this._isType(request, this.rawTypes)) {
      debug('detected raw body')
      // request._raw = await this._parseRaw(request.request)

      request._raw = await new Promise(
        (resolve, reject) => {
          if (request.request._readableState.length < 1024) {
            const body = Buffer.alloc(request.request._readableState.length)
            let ptr = 0
            request.request.on('data', (buffer) => ptr += buffer.copy(body, ptr))
            request.request.on('end', () => resolve(body))
            request.request.on('error', reject)
            request.request.on('aborted', reject)
          } else reject(new Error('Request is too long'))
        }
      )

      await next()
      return
    }

1 Like