Coder Social home page Coder Social logo

Comments (15)

radiumrasheed avatar radiumrasheed commented on May 18, 2024 15

my solution was to use.field()s along with .attach()
(if i remember correctly i even got a warning sometime that .send() and .attach() don't work together)

i've encountered so many issues with multer and multipart form data, so i'd rather handle base64 via json

      return request
        .post('/api/v1/client/1/progress_note')
        .field('answers', progressNoteData.answers)
        .field('duration', progressNoteData.duration)
        .field('templateId', progressNoteData.templateId)
        .field('service', progressNoteData.service)
        .field('service_time', progressNoteData.service_time)
        .attach('attachment', 'test/mock-data/sample-image.png')

from supertest.

 avatar commented on May 18, 2024 6

my solution was to use.field()s along with .attach()
(if i remember correctly i even got a warning sometime that .send() and .attach() don't work together)

i've encountered so many issues with multer and multipart form data, so i'd rather handle base64 via json

from supertest.

wlepinski avatar wlepinski commented on May 18, 2024

Forgot to put the route declaration:

app.post('/test', uploadHandler, function(req, res){
      res.send(201, { name: 'tobi' });
});

from supertest.

gjohnson avatar gjohnson commented on May 18, 2024

You probably didn't use the bodyParser() middleware in express.

from supertest.

SteveNewhouse avatar SteveNewhouse commented on May 18, 2024

Having same problem... let me know if you sorted it @wlepinski

from supertest.

wlepinski avatar wlepinski commented on May 18, 2024

Wow, this issue very old, @SteveNewhouse I really don't remember what I did so try what @gjohnson said. Good luck.

from supertest.

calidion avatar calidion commented on May 18, 2024

how to send image files array and with many fields?

from supertest.

 avatar commented on May 18, 2024

@calidion
+1 how to send image files array and with many fields?

from supertest.

dennishu001 avatar dennishu001 commented on May 18, 2024

You can attach files just as OP described. The problem is that the multipart parser is no longer included in express bodyparser. You need to use a seperate package such as multer to handle multipart/form-data. I just found this out after upgraded my project to express 4.x.

from supertest.

dash7ou avatar dash7ou commented on May 18, 2024

@gjohnson

from supertest.

dash7ou avatar dash7ou commented on May 18, 2024

@SteveNewhouse

from supertest.

tobslob avatar tobslob commented on May 18, 2024

@radiumrasheed .filed only works if the field is a string, how can a field of an array or object be handled? I tried creating multiple fields with the same name, but throws error

from supertest.

tobslob avatar tobslob commented on May 18, 2024

@radiumrasheed .filed only works if the field is a string, how can a field of an array or object be handled? I tried creating multiple fields with the same name but throws an error

This works by stringify the field

from supertest.

radiumrasheed avatar radiumrasheed commented on May 18, 2024

yes, you need to stringify the array

from supertest.

lcundiff avatar lcundiff commented on May 18, 2024

Adding fields didn't seem to change how attachment was handled for me. I still wasn't able to see it when printing out req.files or req.file.

If someone is still looking for a solution, I used "form.parse" as part of multiparty library on my backend (nodejs) to get the files object from req, in which took me forever to realize that the file was stored on the attachment field on "files" as you can see below.

exports.nodeFunction = function (req, res) {
    var form = new multiparty.Form({
        uploadDir: 'app/tmp'
    });
    form.parse(req, function (err, fields, files) { // https://www.npmjs.com/package/multiparty
        var file = files.file ? files.file[0] : files.attachment[0]; 
        // when uploaded on front end, files.file has the file, but files.attachment holds the file when using supertest in mocha. 
    });

Here's my test function as well:
describe('POST', function () { it('Should upload file', async function () { const res = await request(app).post(url) // request = require('supertest') .attach('attachment', path.resolve(__dirname, filename)) // using "path" library .expect(200) }); });

@gjohnson I have the updated body-parser for Express and it still does not work. This issue should be re-opened unless a solution is provided.

from supertest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.