diff --git a/src/request-base.js b/src/request-base.js index 86688c464..efb87370a 100644 --- a/src/request-base.js +++ b/src/request-base.js @@ -666,9 +666,12 @@ RequestBase.prototype.send = function (data) { // merge if (isObject_ && isObject(this._data)) { for (const key in data) { + if (typeof data[key] == "bigint") throw new Error("Cannot serialize BigInt value to json"); if (hasOwn(data, key)) this._data[key] = data[key]; } - } else if (typeof data === 'string') { + } + else if (typeof data === 'bigint') throw new Error("Cannot send value of type BigInt"); + else if (typeof data === 'string') { // default to x-www-form-urlencoded if (!type) this.type('form'); type = this._header['content-type']; diff --git a/test/json.js b/test/json.js index c1d2218a5..3d10d7b4b 100644 --- a/test/json.js +++ b/test/json.js @@ -119,6 +119,32 @@ describe('req.send(Object) as "json"', function () { }); }); + it('should error for BigInt object', (done) => { + try { + request + .post(`${uri}/echo`) + .type('json') + .send({number: 1n}) + throw new Error('Should have thrown error for object with BigInt') + } catch (error) { + assert.strictEqual(error.message, 'Cannot serialize BigInt value to json'); + } + done(); + }); + + it('should error for BigInt primitive', (done) => { + try { + request + .post(`${uri}/echo`) + .type('json') + .send(1n) + throw new Error('Should have thrown error for BigInt primitive') + } catch (error) { + assert.strictEqual(error.message, 'Cannot send value of type BigInt'); + } + done(); + }); + describe('when called several times', () => { it('should merge the objects', (done) => { request