ladjs / superagent

Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
https://ladjs.github.io/superagent/
MIT License
16.59k stars 1.33k forks source link

TypeError setting headers -- must be a string #668

Closed ganna-shmatova closed 7 years ago

ganna-shmatova commented 9 years ago
C:\Users\Ganna\git\CLEARGOALS-Imperium\app\modules\browse_fs\node_modules\supert
est\node_modules\superagent\lib\node\index.js:857
      this.set('Content-Length', Buffer.byteLength(data));
                                        ^
TypeError: Argument must be a string
    at Test.Request.end (C:\Users\x\git\x\app\modules\brow
se_fs\node_modules\supertest\node_modules\superagent\lib\node\index.js:857:41)
    at Test.end (C:\Users\x\git\x\app\modules\browse_fs\no
de_modules\supertest\lib\test.js:125:7)
    at Object.<anonymous> (C:\Users\x\git\x\app\modules\br
owse_fs\test.js:148:3)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

happens with both:

request.get('/upload').expect(200).set('Content-Type', 'multipart/form-data')
    .send({'uploadTo': './test/dir/nonbinary.txt'})
    .attach('file', './test/nonbinary.txt')

and application/x-www-formurlencoded.

Am I missing something...? =S

ganna-shmatova commented 9 years ago

After a lot of research I found an alternative that works:

request.post('/upload').expect(200)
    .field('uploadTo', './test/dir/nonbinary.txt')
    .attach('file', fs.createReadStream('./test/nonbinary.txt'))
    .end(assert('upload'));

turns out you need to attach a read stream to be able to upload the correct headers were automatically sent after this

(the call must also have been post, and i had to use fields method to send in extra data -- send(obj) was not being caught anymore by express)

kornelski commented 7 years ago

Mixing of .send() and .attach() doesn't work. The current version warns about it.