From 4eb74dcebb3a239246153f3d1c5c0fc8552980f3 Mon Sep 17 00:00:00 2001 From: Rohit K Viswanath Date: Sat, 9 Nov 2024 12:54:18 +0530 Subject: [PATCH] fix: expand variables in content type application/x-www-form-urlencoded (#491) * fix: expand variables in content type application/x-www-form-urlencoded * fix: expand variables after parsing urlencoded body --------- Co-authored-by: Seongmin Lee --- lua/rest-nvim/parser/init.lua | 1 + spec/parser/http_parser_spec.lua | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lua/rest-nvim/parser/init.lua b/lua/rest-nvim/parser/init.lua index 9d4ef9b..93ae0b6 100644 --- a/lua/rest-nvim/parser/init.lua +++ b/lua/rest-nvim/parser/init.lua @@ -182,6 +182,7 @@ function parser.parse_body(content_type, body_node, source, context) if content_type and vim.startswith(content_type, "application/x-www-form-urlencoded") then body.__TYPE = "raw" body.data = parse_urlencoded_form(text) + body.data = expand_variables(body.data, context) if not body.data then logger.error("Error while parsing urlencoded form") return nil diff --git a/spec/parser/http_parser_spec.lua b/spec/parser/http_parser_spec.lua index 76440ab..73b1e5a 100644 --- a/spec/parser/http_parser_spec.lua +++ b/spec/parser/http_parser_spec.lua @@ -326,6 +326,23 @@ Authorization: Bearer {{TOKEN}} }]], }, req.body) end) + it("parse with variables in urlencoded body", function () + vim.env["VAR"] = "variable" + local source = [[POST https://example.com +Content-Type: application/x-www-form-urlencoded + +foo={{VAR}} +]] + local _, tree = utils.ts_parse_source(source) + local req_node = assert(tree:root():child(0)) + local req = parser.parse(req_node, source) + assert.not_nil(req) + ---@cast req rest.Request + assert.same({ + __TYPE = "raw", + data = "foo=variable" + }, req.body) + end) it("parse variable declaration", function() local source = "@foo = bar\n" local _, tree = utils.ts_parse_source(source)