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
This commit is contained in:
mircohaug
2020-05-30 00:10:22 +02:00
committed by GitHub
parent 28b7d7c548
commit eaca2f6fd8
2 changed files with 16 additions and 2 deletions

View File

@@ -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( " " )

View File

@@ -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",