question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Send & Attach Are Incompatible.

See original GitHub issue

I was able to get around this by using the low level part() API, but…

When you send form data and have one or more attachments superagent explodes with an obscure “Argument Must be String” error. This is because the content-type gets set to multipart, and there is no multipart formatter to convert it to string, it tries to do a Byte Length operation on the raw JSON to set the content-length.

In fact if you skip that error condition it will send the attachment correctly, but ignore the form-data.

describe '/worms', () ->
  describe 'POST', () ->
    it 'should respond with JSON', (done) ->
      worm = _.first fixtures.worms()
      delete worm.image_url
      delete worm.location
      console.dir 
      supertest(app)
        .post('/worms')
        .attach('image', wormImage)
        .send(worm)
        .expect(400)
        .set('accept', 'application/json')
        .expect('content-type', 'application/json')
        .end (err, res) ->
          throw err if err
          console.dir res.body
          done()

✖ 1 of 1 test failed:

  1. /worms POST should respond with JSON: TypeError: Argument must be a string at Test.Request.end (/Users/vanalsti/Projects/citizen-science-api/node_modules/supertest/node_modules/superagent/lib/node/index.js:607:43) at Test.end (/Users/vanalsti/Projects/citizen-science-api/node_modules/supertest/lib/test.js:121:7) at Context.<anonymous> (/Users/vanalsti/Projects/citizen-science-api/tests/server.coffee:29:173) at Test.Runnable.run (/Users/vanalsti/nvm/v0.8.14/lib/node_modules/mocha/lib/runnable.js:187:15) at Runner.runTest (/Users/vanalsti/nvm/v0.8.14/lib/node_modules/mocha/lib/runner.js:307:10) at Runner.runTests.next (/Users/vanalsti/nvm/v0.8.14/lib/node_modules/mocha/lib/runner.js:353:12) at next (/Users/vanalsti/nvm/v0.8.14/lib/node_modules/mocha/lib/runner.js:235:14) at Runner.hooks (/Users/vanalsti/nvm/v0.8.14/lib/node_modules/mocha/lib/runner.js:244:7) at next (/Users/vanalsti/nvm/v0.8.14/lib/node_modules/mocha/lib/runner.js:192:23) at Runner.hook (/Users/vanalsti/nvm/v0.8.14/lib/node_modules/mocha/lib/runner.js:212:5) at process.startup.processNextTick.process._tickCallback (node.js:244:9)

A less obscure error would be nice. Even nicer would be if send and attach could be compatible.

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
jaketrentcommented, Jun 12, 2013

Request.field() only accepts string values. What’s the recommended method for including json in a multipart request and Request.attach() for images?

3reactions
jchan172commented, Nov 11, 2013

I combined .field and .attach in my test in order to test sending json with an image. Here’s an example:

var should = require(‘should’), supertest = require(‘supertest’); var request = supertest(‘localhost:3000’);

describe(‘upload’, function() { it(‘a file’, function(done) { api.post(‘/your/endpoint’) .field(‘extra_info’, ‘{“in”:“case you want to send json along with your file”}’) .attach(‘image’, ‘path/to/file.jpg’) .end(function(err, res) { res.should.have.status(200) // ‘success’ status done() }); }); });

Take a look at http://stackoverflow.com/questions/10120866/how-to-unit-test-with-a-file-upload-in-mocha

Read more comments on GitHub >

github_iconTop Results From Across the Web

Send & Attach Are Incompatible. · Issue #144 · ladjs/superagent
When you send form data and have one or more attachments superagent explodes with an obscure "Argument Must be String" error.
Read more >
Supertest attaching file - multipart/form-data - Stack Overflow
If you would like to attach files using supertest, you have to use another construction when declaring your fields from form.
Read more >
5 Ways to Fix the "Attachment Failed" Error on Gmail
Try these methods to fix this issue. PC Screen showing Gmail. Gmail is quite a reliable tool when it comes to sending and...
Read more >
Accessing unsupported attachment file types | Help Center
If you cannot access a file sent to you by a user or a lead, you can allow that file type in Settings...
Read more >
Error message received when attempting to add attachment to ...
It's probable that the extension of the file which the user is attempting to attach to the record is not on the configured...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found