fastify-multer,使用Postman测试,request.files未定义

杰里米·格兰杰

我正在尝试创建一个使用fastify消耗multipart / form-data的api。我正在使用该fastify-multer插件来尝试发送文件,以便可以将它们传递给另一个第三方api。现在,我只是想用Postman进行测试,但是当我用png / pdf文件发出发布请求时,request.files则是未定义的。我已经尝试对fastify-multer文档进行了以下修改,并对其进行了修改。我将路由保存在一个单独的目录中,然后fastify-autoload用来将它们引入。server.js正在注册我的所有插件,并且我有一个index.js启动服务器(并且工作正常,因此未在下面包括在内)。这是我的设置:

server.js

const path = require('path')
const fp = require('fastify-plugin')
const autoload = require('fastify-autoload')
const multer = require('fastify-multer')
const upload = multer({ storage: multer.memoryStorage() })

async function registerPlugins(server, config) {
  server
    .register(multer.contentParser)
    .register(require('fastify-cors'), config.cors)
    .register(require('fastify-helmet'))
    .register(require('fastify-sensible'))
    // auto-register all plugins
    .register(autoload, {
      dir: path.join(__dirname, 'plugins'),
      ignorePattern: /.*test.js/,
      options: config
    })
    // auto-register all routes
    .register(autoload, {
      dir: path.join(__dirname, 'routes'),
      ignorePattern: /.*test.js/,
      options: config
    })

  server.decorate('upload', upload)
}

module.exports = fp(registerPlugins)

my.routes.js

const fp = require('fastify-plugin')
const { createMerchantSchema } = require('./schema')

async function myRoutes(server, options) {
  server.post(
    '/merchant/:merchantId/uploadAttachment',
    { preHandler: server.upload.array('files', 8) },
    async (request, reply) => {
      console.log('FILES:')
      console.log(request.files)

      return reply.code(200).send('SUCCESS')
    }
  )
}

module.exports = fp(myRoutes)

我不会为您显示目录结构带来麻烦,但是我还有其他6条路由(此处未显示)可以正常工作,并且服务器启动时没有错误。这是我发送时的控制台输出POST /merchant/12345/uploadAttachment

[nodemon] starting `node index.js`
[1597110502609] INFO  (96323 on 7L6BHR2.agrium.com): Server listening at http://0.0.0.0:3333
[1597110502609] INFO  (96323 on 7L6BHR2.agrium.com): Server running at: http://0.0.0.0:3333
[1597110510972] INFO  (96323 on 7L6BHR2.agrium.com): incoming request
    reqId: 1
    req: {
      "method": "POST",
      "url": "/merchant/123456/uploadAttachment",
      "hostname": "localhost:3333",
      "remoteAddress": "127.0.0.1",
      "remotePort": 51207
    }

FILES:
undefined

[1597110510981] INFO  (96323 on 7L6BHR2.agrium.com): request completed
    reqId: 1
    res: {
      "statusCode": 200
    }
    responseTime: 8.47090208530426

邮递员似乎除了头数据外什么也没有发送。该请求仅为408b:邮递员POST通话

我不是Content-Type: multipart/form-data自己发送标题的。邮递员足够聪明,可以添加它,并且不提供它就意味着我不必越过自己的边界。

我真的不确定这里出了什么问题。有没有人这样设置fastify-multer我有什么想念的吗?我正在拔头发!任何建议将不胜感激。提前致谢!!!

杰里米·格兰杰

Thanks to the creator of fastify-multer, Maksim Sinik, for helping me solve this silly issue. All credit goes to him. I found out that, in fact, fastify-multer works great with fastify-autoload and fastify-plugin. My issue was that in Postman, I wasn't allowing the auto-generated header content-length to be sent with the request:

内容长度标头图片已停用

Internally, fastify-multer uses npm module type-is to help discover the type of the request. If content-length is not sent, content-type is not sent as multipart/form-data and the type-is hasBody method returns null, which causes request.files to be undefined.

Moral of the story: if you want to test fastify-multer (or any other multipart data processor for that matter) with Postman, you must ensure the auto-generated content-length AND content-type header are enabled (selected):

启用了内容类型和内容长度标头的图像

我真的希望这对以后的人有所帮助。我正在拔头发!再次感谢马克西姆。您可以找到他在Github上所做的更多工作

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将NestJS与Fastify适配器和“ fastify-csrf”插件一起使用时,“无法读取未定义的属性'_csrf'”?

来自分类Dev

MULTER - req.files.filename 返回未定义

来自分类Dev

使用Fastify和Fastify-HTTP-Proxy绑定接口

来自分类Dev

使用$ _FILES上传图片时的未定义索引

来自分类Dev

使用$ _FILES上传图片时的未定义索引

来自分类Dev

如何在NestJS中使用Fastify设置自定义Content-Type?

来自分类Dev

ReferenceError:未定义测试

来自分类Dev

使用 Fastify 和 AJV 验证请求模式

来自分类Dev

结果 perf 节点使用 mongo nestJs 或 fastify

来自分类Dev

使用未定义的类型

来自分类Dev

使用未定义的类型

来自分类Dev

multer req.file未定义

来自分类Dev

无法读取未定义的multer的属性

来自分类Dev

使用Vue和Cypress进行测试时,Mirage未定义错误(未定义路由)

来自分类Dev

JUnit测试中“方法$未定义”

来自分类Dev

Selenium测试中未定义$错误

来自分类Dev

在测试主干中未定义el

来自分类Dev

测试的未定义方法“用户”

来自分类Dev

Redux测试-ReferenceError:未定义localStorage

来自分类Dev

测试NoMethodError:未定义的方法

来自分类Dev

验收测试方法访问未定义

来自分类Dev

Jasmine 测试变量是否未定义

来自分类Dev

测试 - 未定义存根服务方法

来自分类Dev

测试 html 输出,但未定义

来自分类Dev

测试人偶中未定义的事实

来自分类Dev

未定义功能 - 银杏测试

来自分类Dev

Kohana未定义的属性:Request :: $ action

来自分类Dev

ActionDispatch :: Request的未定义方法“ flash”

来自分类Dev

未定义MY_PERMISSONS_REQUEST?