Skip to content

Commit

Permalink
Merge pull request #1310 from simov/fix-form-option
Browse files Browse the repository at this point in the history
Revert changes introduced in #1282
  • Loading branch information
nylen committed Dec 10, 2014
2 parents 67b4b1a + 61c9d32 commit 4225f05
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion request.js
Expand Up @@ -1482,7 +1482,7 @@ Request.prototype.json = function (val) {

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

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


tape('application/x-www-form-urlencoded', function(t) {

var server = http.createServer(function(req, res) {

t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded')
t.equal(req.headers.accept, 'application/json')

var data = ''
req.setEncoding('utf8')

req.on('data', function(d) {
data += d
})

req.on('end', function() {
t.equal(data, 'some=url&encoded=data')

res.writeHead(200)
res.end('done')

t.end()
})
})

server.listen(8080, function() {

request.post('http://localhost:8080', {
form: {some: 'url', encoded: 'data'},
json: true
}, function(err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.equal(body, 'done')
server.close()
})
})
})

0 comments on commit 4225f05

Please sign in to comment.