简体   繁体   中英

Why does my nodejs pdf upload code return Status 415 “unsupported Media Type”

Hey so I am using the Node.js framework Hapi.js to make a simple file upload api. After I receive the file I will convert the pdf file to a png. I have been looking for some Hapi.js docs on uploading files but have had no luck. After I receive the file I want to use the Node file system to read it and then pass it to a command line tool.

Here is my route for the conversion:

server.route({
   method: 'POST',
   path: '/convert',
   config: {
        payload: {
            output: 'stream',
            parse: true,
            allow: ['application/json', 'image/jpeg', 'multipart/form-data','application/pdf']
            //allow:'application/json'
        },
         handler:function (request, reply) {
            console.log(request.raw.req);
            var data = request.payload;
            if (data.file) {
                 var name = data.file.hapi.filename;
                 console.log(name);
                 var path = __dirname + "/uploads/" + name;

                 console.log(path);
                 var file = fs.createWriteStream(path);

                 file.on('error', function (err) {
                     console.error(err)
                 });

                 data.file.pipe(file);

                 data.file.on('end', function (err) {
                     var ret = {
                        filename: data.file.hapi.filename,
                        headers: data.file.hapi.headers
                     }
                     console.log(JSON.stringify(ret));
                     reply(JSON.stringify(ret));
                 });
                 data,file.on('data',function(err){
                   console.log('data');
               });
            }
         }
   }
});

I have a simple form with method POST and the action set to my /convert route. Anyone see where my error is? I have not used hapi.js before this.

Here is the header in my post:

headers: 
  { host: 'localhost:8000',
    connection: 'keep-alive',
    'content-length': '31',
    'cache-control': 'max-age=0',
    accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    origin: 'http://localhost:8000',
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/37.0.2062.120 Chrome/37.0.2062.120 Safari/537.36',
    'content-type': 'application/x-www-form-urlencoded',
    referer: 'http://localhost:8000/convert',
    'accept-encoding': 'gzip,deflate',
    'accept-language': 'en-US,en;q=0.8' 
}

As you can see in the headers, the Content-Type header is set to application/x-www-form-urlencoded :

'content-type': 'application/x-www-form-urlencoded'

However it is not in the list of allowed content types:

allow: ['application/json', 'image/jpeg', 'multipart/form-data','application/pdf']

That's why hapi responds with 415. Instead of validating the type of the request, validate the type of the file in the payload:

validate: {
    payload: {
        fileUpload: Joi.object({
            headers: Joi.object({
                'content-type': Joi.string().valid(['application/pdf']).required(),
            }).unknown().required()
        }).unknown()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM