Skip to content

Commit 1b1130f

Browse files
mircohaugmattyb678
mircohaug
authored andcommitted
Add empty data param to cURL if no POST request body was given (swagger-api#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
1 parent 0823b21 commit 1b1130f

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/core/curlify.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ export default function curl( request ){
4747
curlified.push( "-d" )
4848
curlified.push( JSON.stringify( request.get("body") ).replace(/\\n/g, "") )
4949
}
50+
} else if(!request.get("body") && request.get("method") === "POST") {
51+
curlified.push( "-d" )
52+
curlified.push( "\"\"" )
5053
}
5154

5255
return curlified.join( " " )

test/mocha/core/curlify.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ describe("curlify", function() {
2525
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\"}")
2626
})
2727

28+
it("does add a empty data param if no request body given", function() {
29+
var req = {
30+
url: "http://example.com",
31+
method: "POST",
32+
}
33+
34+
let curlified = curl(Im.fromJS(req))
35+
36+
expect(curlified).toEqual("curl -X POST \"http://example.com\" -d \"\"")
37+
})
38+
2839
it("does not change the case of header in curl", function() {
2940
var req = {
3041
url: "http://example.com",
@@ -36,7 +47,7 @@ describe("curlify", function() {
3647

3748
let curlified = curl(Im.fromJS(req))
3849

39-
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"conTenT Type: application/Moar\"")
50+
expect(curlified).toEqual("curl -X POST \"http://example.com\" -H \"conTenT Type: application/Moar\" -d \"\"")
4051
})
4152

4253
it("prints a curl statement with an array of query params", function() {
@@ -145,7 +156,7 @@ describe("curlify", function() {
145156

146157
it("should print a curl with formData that extracts array representation with hashIdx", function() {
147158
// Note: hashIdx = `_**[]${counter}`
148-
// Usage of hashIdx is an internal SwaggerUI method to convert formData array into something curlify can handle
159+
// Usage of hashIdx is an internal SwaggerUI method to convert formData array into something curlify can handle
149160
const req = {
150161
url: "http://example.com",
151162
method: "POST",

0 commit comments

Comments
 (0)