Send & Attach Are Incompatible.
See original GitHub issueI 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:
- /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:
- Created 11 years ago
- Comments:10 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Request.field()
only accepts string values. What’s the recommended method for including json in a multipart request andRequest.attach()
for images?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