hapi — How to Fix “Unsupported Media Type”

The “unsupported media type“ error occurred while uploading a file to a hapi server. The file is an Excel sheet and at first, it seemed like the content type was the issue. But the problem was the multipart upload which comes with a straightforward fix!

hapi Series Overview

Fixing “Unsupported Media Type” Error

The file upload from the frontend used a multipart/form-data configuration. Also, the content type set in the frontend was multipart/form-data. The missing part was the related multipart: true configuration.

The hapi server supports payload configuration options when defining routes. The route fix for the “unsupported media type“ error is the multipart configuration on your route that handles file uploads:

module.exports = {  
  method: 'POST',
  path: '/file-uploads',
  options: {
    payload: {
      maxBytes: 209715200,
      output: 'file',
      parse: true,
      multipart: true     // <-- this fixed the media type error
    }
    handler: async (request, h) => {
      // handle file upload

      return h.response('File uploaded').code(201)
    }
  }
}

Adding multipart: true fixed the file upload problems for us!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.