diff --git a/lib/bhttp.coffee b/lib/bhttp.coffee index 4dbfc44..3e8bfb2 100644 --- a/lib/bhttp.coffee +++ b/lib/bhttp.coffee @@ -223,12 +223,14 @@ preparePayload = (request, response, requestState) -> if (request.options.encodeJSON or request.options.formFields?) and not multipart # We know the payload and its size in advance. debugRequest "got url-encodable form-data" - request.options.headers["content-type"] = "application/x-www-form-urlencoded" if request.options.encodeJSON + debugRequest "... but encodeJSON was set, so we will send JSON instead" + request.options.headers["content-type"] = "application/json" request.payload = JSON.stringify request.options.formFields ? null else if not _.isEmpty request.options.formFields # The `querystring` module copies the key name verbatim, even if the value is actually an array. Things like PHP don't understand this, and expect every array-containing key to be suffixed with []. We'll just append that ourselves, then. + request.options.headers["content-type"] = "application/x-www-form-urlencoded" request.payload = querystring.stringify formFixArray(request.options.formFields) else request.payload = "" diff --git a/lib/bhttp.js b/lib/bhttp.js index 862ee0f..40e6c16 100644 --- a/lib/bhttp.js +++ b/lib/bhttp.js @@ -254,10 +254,12 @@ preparePayload = function(request, response, requestState) { if ((_ref = request.options.method) === "post" || _ref === "put" || _ref === "patch") { if ((request.options.encodeJSON || (request.options.formFields != null)) && !multipart) { debugRequest("got url-encodable form-data"); - request.options.headers["content-type"] = "application/x-www-form-urlencoded"; if (request.options.encodeJSON) { + debugRequest("... but encodeJSON was set, so we will send JSON instead"); + request.options.headers["content-type"] = "application/json"; request.payload = JSON.stringify((_ref1 = request.options.formFields) != null ? _ref1 : null); } else if (!_.isEmpty(request.options.formFields)) { + request.options.headers["content-type"] = "application/x-www-form-urlencoded"; request.payload = querystring.stringify(formFixArray(request.options.formFields)); } else { request.payload = ""; diff --git a/package.json b/package.json index 018cdf6..8e3f552 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bhttp", - "version": "1.1.1", + "version": "1.1.2", "description": "A sane HTTP client library for Node.js with Streams2 support.", "main": "index.js", "scripts": {