Skip to content

Commit 51d115e

Browse files
feat: external body support
1 parent 78ad115 commit 51d115e

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function builder.file(file)
197197
return
198198
end
199199
-- FIXME: should normalize/expand the file path
200-
return { "-d", "@" .. file }
200+
return { "--data-binary", "@" .. file }
201201
end
202202

203203
---@package
@@ -230,7 +230,7 @@ function builder.build(request)
230230
if request.body.__TYPE == "form" then
231231
table.insert(args, builder.form(request.body.data))
232232
elseif request.body.__TYPE == "external" then
233-
-- TODO: external body (how intellij works?)
233+
table.insert(args, builder.file(request.body.data.path))
234234
else
235235
table.insert(args, builder.raw_body(request.body.data))
236236
end

lua/rest-nvim/parser/init.lua

+6-2
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,14 @@ function M.parse_body(body_node, source, context)
9898
end
9999
end
100100
elseif body.__TYPE == "external" then
101+
local path = assert(get_node_field_text(body_node, "path", source))
102+
-- TODO: make sure expand happens in `.http` file
103+
path = vim.fs.normalize(vim.fs.joinpath(vim.fn.expand("%:h"), path))
101104
body.data = {
102-
name = assert(get_node_field_text(body_node, "name", source)),
103-
path = assert(get_node_field_text(body_node, "path", source)),
105+
name = get_node_field_text(body_node, "name", source),
106+
path = path,
104107
}
108+
vim.print(body.data.path)
105109
elseif body.__TYPE == "graphql" then
106110
logger.error("graphql body is not supported yet")
107111
end

spec/client_curl_cli_spec.lua

+17
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ describe("curl cli builder", function()
5151
})
5252
assert.same({ "http://localhost:8000", "-X", "POST", "--data-raw", json_text }, args)
5353
end)
54+
it("from POST request with external body", function ()
55+
local args = builder.build({
56+
context = Context:new(),
57+
method = "POST",
58+
url = "http://localhost:8000",
59+
headers = {
60+
},
61+
handlers = {},
62+
body = {
63+
__TYPE = "external",
64+
data = {
65+
path = "spec/test_server/post_json.json"
66+
},
67+
},
68+
})
69+
assert.same({ "http://localhost:8000", "-X", "POST", "--data-binary", "@spec/test_server/post_json.json" }, args)
70+
end)
5471
end)
5572

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

spec/test_server/post_json.http

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
POST http://localhost:8000/post/json
2+
Content-Type: application/json
3+
4+
< ./post_json.json

spec/test_server/post_json.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"asdf": 123
3+
}

0 commit comments

Comments
 (0)