Skip to content

Commit 3200046

Browse files
feat(curl.cli): basic body support
1 parent dded8d6 commit 3200046

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lua/rest-nvim/client/curl/cli.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,15 @@ function builder.build(request)
225225
table.insert(args, request.url)
226226
table.insert(args, builder.method(request.method))
227227
table.insert(args, builder.headers(request.headers))
228-
-- TODO: body
228+
if request.body then
229+
if request.body.__TYPE == "form" then
230+
table.insert(args, builder.form(request.body.data))
231+
elseif request.body.__TYPE == "external" then
232+
-- TODO: external body (how intellij works?)
233+
else
234+
table.insert(args, builder.raw_body(request.body.data))
235+
end
236+
end
229237
-- TODO: auth?
230238
builder.http_version(request.http_version)
231239
return vim.iter(args):flatten():totable()

spec/client_curl_cli_spec.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,38 @@ describe("curl cli builder", function()
1919
})
2020
assert.same({ "http://localhost:8000", "-X", "GET" }, args)
2121
end)
22+
it("from POST request with form body", function ()
23+
local args = builder.build({
24+
context = Context:new(),
25+
method = "POST",
26+
url = "http://localhost:8000",
27+
headers = {},
28+
handlers = {},
29+
body = {
30+
__TYPE = "form",
31+
data = {
32+
foo = "bar",
33+
},
34+
},
35+
})
36+
assert.same({ "http://localhost:8000", "-X", "POST", "-F", "foo=bar" }, args)
37+
end)
38+
it("from POST request with json body", function ()
39+
local json_text = [[{ "string": "foo", "number": 100, "array": [1, 2, 3], "json": { "key": "value" } }]]
40+
local args = builder.build({
41+
context = Context:new(),
42+
method = "POST",
43+
url = "http://localhost:8000",
44+
headers = {
45+
},
46+
handlers = {},
47+
body = {
48+
__TYPE = "json",
49+
data = json_text,
50+
},
51+
})
52+
assert.same({ "http://localhost:8000", "-X", "POST", "--data-raw", json_text }, args)
53+
end)
2254
end)
2355

2456
describe("curl cli response parser", function()

0 commit comments

Comments
 (0)