diff --git a/lua/rest-nvim/client/curl/cli.lua b/lua/rest-nvim/client/curl/cli.lua index d59e5e5..6f09356 100644 --- a/lua/rest-nvim/client/curl/cli.lua +++ b/lua/rest-nvim/client/curl/cli.lua @@ -299,7 +299,11 @@ function builder.build(req, ignore_stats) insert(args, builder.cookies(req.cookies)) if req.body then if req.body.__TYPE == "external" then - insert(args, builder.file(req.body.data.path)) + if req.body.data.content then + insert(args, builder.raw_body(req.body.data.content)) + else + insert(args, builder.file(req.body.data.path)) + end elseif req.body.__TYPE == "multipart_form_data" then log.error("multipart-form-data body is not supportted yet") elseif vim.list_contains({ "json", "xml", "raw", "graphql" }, req.body.__TYPE) then diff --git a/lua/rest-nvim/parser/init.lua b/lua/rest-nvim/parser/init.lua index 50b2d8d..f123389 100644 --- a/lua/rest-nvim/parser/init.lua +++ b/lua/rest-nvim/parser/init.lua @@ -136,6 +136,14 @@ function parser.parse_body(content_type, body_node, source, context) name = get_node_field_text(body_node, "name", source), path = path, } + local body_text = vim.treesitter.get_node_text(body_node, source) + if vim.startswith(body_text, "<@") then + logger.debug("external body with '<@' prefix") + return body + end + local file_content = utils.read_file(path) + file_content = expand_variables(file_content, context) + body.data.content = file_content elseif node_type == "graphql_body" then body.__TYPE = "graphql" local query_text = vim.treesitter.get_node_text(assert(body_node:named_child(0)), source) diff --git a/spec/examples/examples_spec.lua b/spec/examples/examples_spec.lua index fc5bbf3..67e883c 100644 --- a/spec/examples/examples_spec.lua +++ b/spec/examples/examples_spec.lua @@ -109,7 +109,7 @@ describe("builtin request hooks", function() it("with external body", function() local source = open("spec/examples/request_body/external_body.http") local _, tree = utils.ts_parse_source(source) - local req_node = assert(tree:root():child(0)) + local req_node = assert(tree:root():child(1)) local req = assert(parser.parse(req_node, source)) _G.rest_request = req vim.api.nvim_exec_autocmds("User", { diff --git a/spec/examples/request_body/external_body.http b/spec/examples/request_body/external_body.http index 7bb0be0..319948f 100644 --- a/spec/examples/request_body/external_body.http +++ b/spec/examples/request_body/external_body.http @@ -1,5 +1,12 @@ +@bar=baz ### External body POST https://example.com:8080/api/html/post # rest.nvim can guess and fill the Content-Type header from file path < ./input.json + +### External body (raw) +POST https://example.com:8080/api/html/post +# rest.nvim can guess and fill the Content-Type header from file path + +<@ ./input.json diff --git a/spec/examples/request_body/input.json b/spec/examples/request_body/input.json new file mode 100644 index 0000000..dd12586 --- /dev/null +++ b/spec/examples/request_body/input.json @@ -0,0 +1,3 @@ +{ + "foo": "{{bar}}" +} diff --git a/spec/parser/http_parser_spec.lua b/spec/parser/http_parser_spec.lua index 9f6de30..fe3c95f 100644 --- a/spec/parser/http_parser_spec.lua +++ b/spec/parser/http_parser_spec.lua @@ -186,7 +186,7 @@ key5 = value5 -- external body can be only sourced when local source = open("spec/examples/request_body/external_body.http") local _, tree = utils.ts_parse_source(source) - local req_node = assert(tree:root():child(0)) + local req_node = assert(tree:root():child(1)) assert.same({ method = "POST", url = "https://example.com:8080/api/html/post", @@ -194,6 +194,27 @@ key5 = value5 cookies = {}, handlers = {}, name = "External body", + body = { + __TYPE = "external", + data = { + path = "spec/examples/request_body/input.json", + content = '{\n "foo": "baz"\n}\n', + }, + }, + }, parser.parse(req_node, source)) + end) + it("parse external body (raw)", function() + -- external body can be only sourced when + local source = open("spec/examples/request_body/external_body.http") + local _, tree = utils.ts_parse_source(source) + local req_node = assert(tree:root():child(2)) + assert.same({ + method = "POST", + url = "https://example.com:8080/api/html/post", + headers = {}, + cookies = {}, + handlers = {}, + name = "External body (raw)", body = { __TYPE = "external", data = {