From 1b1130fa94ff9d2a14c48c797ae902b111f38eb8 Mon Sep 17 00:00:00 2001 From: mircohaug Date: Sat, 30 May 2020 00:10:22 +0200 Subject: [PATCH] Add empty data param to cURL if no POST request body was given (#6017) * Add empty data param to cURL if no request body was given Some middleware applications do not allow POST requests without a content-length header. By adding a empty data parameter to the curl command, the content-length header will be set by curl. Besides this it is more obvious to the user that no request body is sent. * use double quotes like the rest of the curl command --- src/core/curlify.js | 3 +++ test/mocha/core/curlify.js | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/core/curlify.js b/src/core/curlify.js index c12f18965a3..bb853b9e3af 100644 --- a/src/core/curlify.js +++ b/src/core/curlify.js @@ -47,6 +47,9 @@ export default function curl( request ){ curlified.push( "-d" ) curlified.push( JSON.stringify( request.get("body") ).replace(/\\n/g, "") ) } + } else if(!request.get("body") && request.get("method") === "POST") { + curlified.push( "-d" ) + curlified.push( "\"\"" ) } return curlified.join( " " ) diff --git a/test/mocha/core/curlify.js b/test/mocha/core/curlify.js index 61aad97486f..44ee88b143b 100644 --- a/test/mocha/core/curlify.js +++ b/test/mocha/core/curlify.js @@ -25,6 +25,17 @@ describe("curlify", function() { expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"Accept: application/json\" -H \"content-type: application/json\" -d {\"id\":0,\"name\":\"doggie\",\"status\":\"available\"}") }) + it("does add a empty data param if no request body given", function() { + var req = { + url: "http://example.com", + method: "POST", + } + + let curlified = curl(Im.fromJS(req)) + + expect(curlified).toEqual("curl -X POST \"http://example.com\" -d \"\"") + }) + it("does not change the case of header in curl", function() { var req = { url: "http://example.com", @@ -36,7 +47,7 @@ describe("curlify", function() { let curlified = curl(Im.fromJS(req)) - expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"conTenT Type: application/Moar\"") + expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"conTenT Type: application/Moar\" -d \"\"") }) it("prints a curl statement with an array of query params", function() { @@ -145,7 +156,7 @@ describe("curlify", function() { it("should print a curl with formData that extracts array representation with hashIdx", function() { // Note: hashIdx = `_**[]${counter}` - // Usage of hashIdx is an internal SwaggerUI method to convert formData array into something curlify can handle + // Usage of hashIdx is an internal SwaggerUI method to convert formData array into something curlify can handle const req = { url: "http://example.com", method: "POST",