【问题标题】:How to attach multiple form-data fields with supertest?如何使用 supertest 附加多个表单数据字段?
【发布时间】:2020-12-17 01:29:35
【问题描述】:

我正在执行集成测试,我有一个场景,我必须在表单数据中提供字段。我可以在 .attach 中提供一个密钥和文件。在我的情况下,我必须在同一个请求中提供表单数据中的五个键和值。如何实现?

我已经完成的示例代码。

      const response = await request(server)
        .post('url for the request')
        .set({
          'Content-Type': 'application/json',
        })
        .attach('key', 'file');

【问题讨论】:

    标签: javascript node.js jestjs supertest


    【解决方案1】:

    您可以使用.field() 添加其他基于文本的字段(有关详细信息,请参阅docs):

      const response = await request(server)
            .post('url for the request')
            .set({
              'Content-Type': 'application/json',
            })
            .field('category', 'Product')
            .field('type', 'Image')
            .field('category', 'Product')
            .field('entity_no', '12354')
            .attach('file', '/path/to/file');
    

    【讨论】:

      猜你喜欢
      • 2019-11-29
      • 1970-01-01
      • 2020-05-06
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 2017-08-21
      相关资源
      最近更新 更多