Skip to content

Commit

Permalink
Enable non-object JSON bodies
Browse files Browse the repository at this point in the history
Updated JSON requests so they can contain bodies that are not objects
  • Loading branch information
apoco committed Nov 21, 2014
1 parent e543acf commit b0ded10
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
node_modules
.idea
2 changes: 1 addition & 1 deletion request.js
Expand Up @@ -1481,7 +1481,7 @@ Request.prototype.json = function (val) {

self._json = true
if (typeof val === 'boolean') {
if (typeof self.body === 'object') {
if (self.body !== undefined) {
self.body = safeStringify(self.body)
if (!self.hasHeader('content-type')) {
self.setHeader('content-type', 'application/json')
Expand Down
33 changes: 33 additions & 0 deletions tests/test-json-request.js
@@ -0,0 +1,33 @@
'use strict'

var server = require('./server')
, stream = require('stream')
, request = require('../index')
, tape = require('tape')

var s = server.createServer()

tape('setup', function(t) {
s.listen(s.port, function() {
t.end()
})
})

tape('test testPutBoolean', function(t) {
s.on('/testPutBoolean', server.createPostValidator('true', 'application/json'))
var opts = {
method: 'PUT',
uri: s.url + '/testPutBoolean',
json: true,
body: true
}
request(opts, function (err, resp, body) {
t.equal(err, null)
t.end()
})
})

tape('cleanup', function(t) {
s.close()
t.end()
})

0 comments on commit b0ded10

Please sign in to comment.