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.56k stars 1.33k forks source link

post set content-length error #1730

Open sherryhhx opened 2 years ago

sherryhhx commented 2 years ago

The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of Object at Function.byteLength (buffer.js:724:11) at Request._end (xxxx/node_modules/superagent/lib/node/index.js:804:84) at Request.end (xxx/node_modules/superagent/lib/node/index.js:777:15)

Woodz commented 2 hours ago

To clarify this bug, this happens when trying to call .send with an object, e.g.

await request(app.getHttpServer())
            .post('/foo/bar')
            .send({a: 1});

The workaround is to call JSON.stringify on the payload, e.g.

await request(app.getHttpServer())
            .post('/foo/bar')
            .send(JSON.stringify({a: 1}));

Superagent should catch this by tightening TS typing and should have a nicer error message