From 93b1d532e8e44512186be2dccbbf85d712aacfa1 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Mon, 27 Apr 2015 17:07:02 +0200 Subject: [PATCH] v1.2.0: Allow payloads for custom HTTP verbs --- lib/bhttp.coffee | 5 ++++- lib/bhttp.js | 4 +++- package.json | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/bhttp.coffee b/lib/bhttp.coffee index 8a93876..c974ed2 100644 --- a/lib/bhttp.coffee +++ b/lib/bhttp.coffee @@ -218,7 +218,7 @@ preparePayload = (request, response, requestState) -> if request.options.encodeJSON and containsStreams return Promise.reject() new bhttpErrors.ConflictingOptionsError "Sending a JSON-encoded payload containing data from a stream is not currently supported.", undefined, "Either don't use encodeJSON, or read your stream into a string or Buffer." - if request.options.method in ["post", "put", "patch"] + if request.options.method not in ["get", "head", "delete"] # Prepare the payload, and set the appropriate headers. if (request.options.encodeJSON or request.options.formFields?) and not multipart # We know the payload and its size in advance. @@ -293,6 +293,9 @@ preparePayload = (request, response, requestState) -> debugRequest "length for inputBuffer is %s", request.payload.length request.options.headers["content-length"] = request.payload.length + return Promise.resolve() + else + # No payload specified. return Promise.resolve() else # GET, HEAD and DELETE should not have a payload. While technically not prohibited by the spec, it's also not specified, and we'd rather not upset poorly-compliant webservers. diff --git a/lib/bhttp.js b/lib/bhttp.js index fa39e2d..4029d08 100644 --- a/lib/bhttp.js +++ b/lib/bhttp.js @@ -251,7 +251,7 @@ preparePayload = function(request, response, requestState) { if (request.options.encodeJSON && containsStreams) { return Promise.reject()(new bhttpErrors.ConflictingOptionsError("Sending a JSON-encoded payload containing data from a stream is not currently supported.", void 0, "Either don't use encodeJSON, or read your stream into a string or Buffer.")); } - if ((_ref = request.options.method) === "post" || _ref === "put" || _ref === "patch") { + if ((_ref = request.options.method) !== "get" && _ref !== "head" && _ref !== "delete") { if ((request.options.encodeJSON || (request.options.formFields != null)) && !multipart) { debugRequest("got url-encodable form-data"); if (request.options.encodeJSON) { @@ -324,6 +324,8 @@ preparePayload = function(request, response, requestState) { debugRequest("length for inputBuffer is %s", request.payload.length); request.options.headers["content-length"] = request.payload.length; return Promise.resolve(); + } else { + return Promise.resolve(); } } else { return Promise.resolve(); diff --git a/package.json b/package.json index 2eff0fe..34b9b43 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bhttp", - "version": "1.1.3", + "version": "1.2.0", "description": "A sane HTTP client library for Node.js with Streams2 support.", "main": "index.js", "scripts": {