ralvez
ralvez

Reputation: 1

HapiJS not accepting form-data requests

{ "statusCode": 415, "error": "Unsupported Media Type", "message": "Unsupported Media Type" }

It seems as if I need something added to my hapi server in order to accept form-data. Here is my server file:

const hapi = require('@hapi/hapi');
// const HapiAuthCookie = require('hapi-auth-cookie');
const CORS = { cors: true };
const SERVER_SETTINGS = { host: 'localhost', port: 8040, routes: CORS };

const server = hapi.server(SERVER_SETTINGS);

const endpoints = require('./routes.js');

// connect to database
require('../db/dbconn.js');

module.exports = {
    start: async() => {
        // plugin registration
        await server.register([]);

        server.route(endpoints,
            { prefix: '/dart-api' }
        );
        // start server
        try {
            await server.start();
            console.log(`Server running on: ${server.info.uri}`);
        } catch (error) {
            console.log(error);
        }
    }

};

// Graceful server stop
process.on('SIGINT', () => {
    /* eslint-disable-next-line */
    console.warn('\n\n>> Stopping Hapi server ...');
    server.stop({ timeout: 100 }).then((err) => {
        if (err) {
            console.log('Hapi server stopped');
            throw err;
        }
    });
}); // server.stop {ends}

process.on('unhandledRejection', (err) => {
    console.log(err);
    throw err;
});

I'll appreciate some help. Thanks in advance.

Update: I've done some testing with different clients with the same result. Here is the response when I use curl:

    curl http://localhost:8040/users/avatars -X PUT -F 'files=@/home/ralvez/.downloads/ICONS/ricavatar.png' -vvv
*   Trying 127.0.0.1:8040...
* Connected to localhost (127.0.0.1) port 8040 (#0)
> PUT /users/avatars HTTP/1.1
> Host: localhost:8040
> User-Agent: curl/7.70.0
> Accept: */*
> Content-Length: 17433
> Content-Type: multipart/form-data; boundary=------------------------0e59b1780748d1d6
>
* We are completely uploaded and fine
* Mark bundle as not supporting multiuse
< HTTP/1.1 415 Unsupported Media Type
< content-type: application/json; charset=utf-8
< vary: origin
< access-control-expose-headers: WWW-Authenticate,Server-Authorization
< cache-control: no-cache
< content-length: 86
< Date: Fri, 22 May 2020 17:27:01 GMT
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
{"statusCode":415,"error":"Unsupported Media Type","message":"Unsupported Media Type"}

This make no sense to me since my handler is like this:

{
    method: 'PUT',
    path: '/users/avatars',
    handler: users.updateAvatar,
    options: {
        payload: {
            output: 'stream',
            parse: true,
            allow: 'multipart/form-data'
        }
    }
},

there is no doubt in my mind that this is ready to accept form-data, yet it is rejecting with "statusCode": 415, "error": "Unsupported Media Type", what gives?!

Upvotes: 0

Views: 834

Answers (1)

ralvez
ralvez

Reputation: 1

OK. After much searching and after realizing this HAD to be related to the version of HapiJS I'm using I found someone else had the same problem. Here is the link: [Fix Hapi version 19.0.3 error 415 unsupported media type upload file with multipart/form-data

So I decided to change my route as follows:

{
    method: 'PUT',
    path: '/users/avatars',
    handler: users.updateAvatar,
    options: {
        payload: {
            output: 'stream',
            parse: true,
            allow: 'multipart/form-data',
            multipart: true
        }
    }
},

and it works.

A shorter version also works:

{
    method: 'PUT',
    path: '/users/avatars',
    handler: users.updateAvatar,
    options: {
        payload: {
            multipart: true
        }
    }
},

Upvotes: 0

Related Questions