Browse Source

v1.2.0: Allow payloads for custom HTTP verbs

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

5
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.

4
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();

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

Loading…
Cancel
Save