diff --git a/src/core/plugins/request-snippets/fn.js b/src/core/plugins/request-snippets/fn.js index d456426c..7da9115e 100644 --- a/src/core/plugins/request-snippets/fn.js +++ b/src/core/plugins/request-snippets/fn.js @@ -103,9 +103,10 @@ const curlify = (request, escape, newLine, ext = "") => { } } - if (request.get("body")) { + const body = request.get("body") + if (body) { if (isMultipartFormDataRequest && ["POST", "PUT", "PATCH"].includes(request.get("method"))) { - for (let [k, v] of request.get("body").entrySeq()) { + for (let [k, v] of body.entrySeq()) { let extractedKey = extractKey(k) addNewLine() addIndent() @@ -116,11 +117,15 @@ const curlify = (request, escape, newLine, ext = "") => { addWords(`${extractedKey}=${v}`) } } + } else if(body instanceof win.File) { + addNewLine() + addIndent() + addWordsWithoutLeadingSpace(`--data-binary '@${body.name}'`) } else { addNewLine() addIndent() addWordsWithoutLeadingSpace("-d ") - let reqBody = request.get("body") + let reqBody = body if (!Map.isMap(reqBody)) { if (typeof reqBody !== "string") { reqBody = JSON.stringify(reqBody) @@ -130,7 +135,7 @@ const curlify = (request, escape, newLine, ext = "") => { addWordsWithoutLeadingSpace(getStringBodyOfMap(request)) } } - } else if (!request.get("body") && request.get("method") === "POST") { + } else if (!body && request.get("method") === "POST") { addNewLine() addIndent() addWordsWithoutLeadingSpace("-d ''") diff --git a/test/unit/core/curlify.js b/test/unit/core/curlify.js index aa168d07..d67b122d 100644 --- a/test/unit/core/curlify.js +++ b/test/unit/core/curlify.js @@ -220,6 +220,21 @@ describe("curlify", function () { expect(curlified).toEqual("curl -X 'POST' \\\n 'http://example.com' \\\n -H 'content-type: multipart/form-data' \\\n -F 'id=123' \\\n -F 'file=@file.txt'") }) + it("should print a curl with data-binary if body is instance of File and it is not a multipart form data request", function () { + let file = new win.File([""], "file.txt", { type: "" }) + + let req = { + url: "http://example.com", + method: "POST", + headers: { "content-type": "application/octet-stream" }, + body: file + } + + let curlified = curl(Im.fromJS(req)) + + expect(curlified).toEqual("curl -X 'POST' \\\n 'http://example.com' \\\n -H 'content-type: application/octet-stream' \\\n --data-binary '@file.txt'") + }) + it("prints a curl post statement from an object", function () { let req = { url: "http://example.com",