Skip to content

Commit

Permalink
fix: unset content length in rest-json when payload is undefined (#4219)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Sep 27, 2022
1 parent 2728d90 commit 62f23c6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-rest-json-a441c484.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "bugfix",
"category": "rest-json",
"description": "unset content length in rest-json when payload is undefined"
}
1 change: 1 addition & 0 deletions lib/event_listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ AWS.EventListeners = {
add('BUILD', 'build', svc.buildRequest);
add('EXTRACT_DATA', 'extractData', svc.extractData);
add('EXTRACT_ERROR', 'extractError', svc.extractError);
add('UNSET_CONTENT_LENGTH', 'afterBuild', svc.unsetContentLength);
}),

RestXml: new SequentialExecutor().addNamedListeners(function(add) {
Expand Down
17 changes: 15 additions & 2 deletions lib/protocol/rest_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ var Json = require('./json');
var JsonBuilder = require('../json/builder');
var JsonParser = require('../json/parser');

var METHODS_WITHOUT_BODY = ['GET', 'HEAD', 'DELETE'];

function unsetContentLength(req) {
var payloadMember = util.getRequestPayloadShape(req);
if (
payloadMember === undefined &&
METHODS_WITHOUT_BODY.indexOf(req.httpRequest.method) >= 0
) {
delete req.httpRequest.headers['Content-Length'];
}
}

function populateBody(req) {
var builder = new JsonBuilder();
var input = req.service.api.operations[req.operation].input;
Expand Down Expand Up @@ -40,7 +52,7 @@ function buildRequest(req) {
Rest.buildRequest(req);

// never send body payload on GET/HEAD/DELETE
if (['GET', 'HEAD', 'DELETE'].indexOf(req.httpRequest.method) < 0) {
if (METHODS_WITHOUT_BODY.indexOf(req.httpRequest.method) < 0) {
populateBody(req);
}
}
Expand Down Expand Up @@ -89,5 +101,6 @@ function extractData(resp) {
module.exports = {
buildRequest: buildRequest,
extractError: extractError,
extractData: extractData
extractData: extractData,
unsetContentLength: unsetContentLength
};
6 changes: 6 additions & 0 deletions test/protocol/protocol.spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 62f23c6

Please sign in to comment.