Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix changes introduced in https://github.com/request/request/pull/1282 #1310

Merged
merged 3 commits into from Dec 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
})
})
})