Search code examples
javascriptnode.jsjestjssupertest

How to attach multiple form-data fields with supertest?


I am performing integration test, I have a scenario where i have to provide fields in form-data. I can able to provide one key and file at .attach. in my case i have to provide five keys and values in form-data in the same request. how to achieve it?

enter image description here

sample code i have done to perform.

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

Solution

  • You can use .field() to add the other text-based fields (see the docs for more details):

      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');