Skip to content

Commit

Permalink
Add empty data param to cURL if no request body was given
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Mirco Haug committed May 18, 2020
1 parent b85ae03 commit 90e2074
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/core/curlify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( " " )
Expand Down
15 changes: 13 additions & 2 deletions test/mocha/core/curlify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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() {
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 90e2074

Please sign in to comment.