Browse Source

v1.1.2: Correctly send an application/json content-type for JSON-encoded request payloads

pull/4/head
Sven Slootweg 7 years ago
parent
commit
cb607eeb86
  1. 4
      lib/bhttp.coffee
  2. 4
      lib/bhttp.js
  3. 2
      package.json

4
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 = ""

4
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 = "";

2
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": {

Loading…
Cancel
Save