Hapi 19.0.3上传文件报415不支持媒体类型问题排查与解决
I recently ran into a frustrating 415 Unsupported Media Type error while trying to upload files using multipart/form-data with Hapi 19.0.3. After searching around without finding a working solution, I turned to debugging in VS Code to get to the bottom of it.
I traced the issue to the @hapi/subtext/lib/index.js file, where I found this key code snippet:
if (contentType.mime === 'multipart/form-data') { if (options.multipart === false) {// Defaults to true throw Boom.unsupportedMediaType(); } return await internals.multipart(req, options, source, contentType); }
It turned out the error was being thrown because even though multipart defaults to true, my route's payload configuration must have been overriding this behavior implicitly.
The fix was straightforward: explicitly set multipart: true in the route's payload options. Here's the full configuration that resolved the issue for me:
{ payload: { maxBytes: 1024 * 1024 * 100, parse: true, output: 'data', allow: 'multipart/form-data', multipart: true } }
Once I added that explicit multipart: true line to the payload settings, the 415 error vanished and file uploads started working as expected.
I'm sharing this solution in case other developers hit the same roadblock—sometimes explicitly defining a default value is all it takes to resolve these tricky media type issues in Hapi.
内容的提问来源于stack exchange,提问作者Tran Hoang Hiep

