Skip to content

Commit 90c46f7

Browse files
feat: treat form-urlencoded as raw-body
1 parent 7d1058f commit 90c46f7

File tree

5 files changed

+20
-35
lines changed

5 files changed

+20
-35
lines changed

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,6 @@ function builder.data_body(body)
211211
return kv_to_list(body, "-d", "=")
212212
end
213213

214-
---@package
215-
---@param form table<string,string>?
216-
---@return string[]? args
217-
function builder.form(form)
218-
if not form then
219-
return
220-
end
221-
return kv_to_list(form, "-F", "=")
222-
end
223-
224214
function builder.file(file)
225215
if not file then
226216
return
@@ -284,12 +274,16 @@ function builder.build(req, ignore_stats)
284274
insert(args, builder.headers(req.headers))
285275
insert(args, builder.cookies(req.cookies))
286276
if req.body then
287-
if req.body.__TYPE == "form" then
288-
insert(args, builder.form(req.body.data))
289-
elseif req.body.__TYPE == "external" then
277+
if req.body.__TYPE == "external" then
290278
insert(args, builder.file(req.body.data.path))
291-
else
279+
elseif req.body.__TYPE == "graphql" then
280+
log.error("graqphql body is not supportted yet")
281+
elseif req.body.__TYPE == "multipart_form_data" then
282+
log.error("multipart-form-data body is not supportted yet")
283+
elseif vim.list_contains({ "json", "xml", "raw" }, req.body.__TYPE) then
292284
insert(args, builder.raw_body(req.body.data))
285+
else
286+
log.error(("unkown body type: '%s'"):format(req.body.__TYPE))
293287
end
294288
end
295289
if config.request.skip_ssl_verification then

lua/rest-nvim/parser/init.lua

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,17 @@ local function validate_xml(str)
9090
end
9191

9292
---@param str string
93-
---@return table<string,string>?
93+
---@return string?
9494
local function parse_urlencoded_form(str)
95-
local form = {}
9695
local query_pairs = vim.split(str, "&")
97-
for _, query in ipairs(query_pairs) do
96+
return vim.iter(query_pairs):map(function (query)
9897
local key, value = query:match("([^=]+)=?(.*)")
9998
if not key then
10099
-- TODO: error
101100
return nil
102101
end
103-
form[vim.trim(key)] = vim.trim(value)
104-
end
105-
return form
102+
return vim.trim(key) .. "=" .. vim.trim(value)
103+
end):join("&")
106104
end
107105

108106
---@param content_type string?
@@ -150,7 +148,7 @@ function parser.parse_body(content_type, body_node, source, context)
150148
-- TODO: exclude comments from text
151149
local text = vim.treesitter.get_node_text(body_node, source)
152150
if content_type and vim.startswith(content_type, "application/x-www-form-urlencoded") then
153-
body.__TYPE = "form"
151+
body.__TYPE = "raw"
154152
body.data = parse_urlencoded_form(text)
155153
if not body.data then
156154
-- TODO: parsing urlencoded form failed

lua/rest-nvim/request.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local jar = require("rest-nvim.cookie_jar")
1212
local clients = require("rest-nvim.client")
1313

1414
---@class rest.Request.Body
15-
---@field __TYPE "json"|"xml"|"raw"|"graphql"|"multipart_form_data"|"form"|"external"
15+
---@field __TYPE "json"|"xml"|"raw"|"graphql"|"multipart_form_data"|"external"
1616
---@field data any
1717

1818
---@class rest.Request

spec/client/curl/cli_spec.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ describe("curl cli builder", function()
4545
cookies = {},
4646
handlers = {},
4747
body = {
48-
__TYPE = "form",
49-
data = {
50-
foo = "bar",
51-
},
48+
__TYPE = "raw",
49+
data = "field1=value1&field2=value2",
5250
},
5351
})
54-
assert.same({ "http://localhost:8000", "-X", "POST", "-F", "foo=bar", "-w", STAT_FORMAT }, args)
52+
assert.same({ "http://localhost:8000", "-X", "POST", "--data-raw", "field1=value1&field2=value2", "-w", STAT_FORMAT }, args)
5553
end)
5654
it("from POST request with json body", function ()
5755
local json_text = [[{ "string": "foo", "number": 100, "array": [1, 2, 3], "json": { "key": "value" } }]]

spec/parser/http_parser_spec.lua

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,8 @@ key5 = value5
3434
cookies = {},
3535
handlers = {},
3636
body = {
37-
__TYPE = "form",
38-
data = {
39-
key1 = "value1",
40-
key2 = "value2",
41-
key3 = "value3",
42-
key4 = "value4",
43-
key5 = "value5",
44-
},
37+
__TYPE = "raw",
38+
data = "key1=value1&key2=value2&key3=value3&key4=value4&key5=value5",
4539
},
4640
}, parser.parse(req_node, source))
4741
end)
@@ -58,6 +52,7 @@ key5 = value5
5852
},
5953
cookies = {},
6054
handlers = {},
55+
name = "post_with_external_body#1",
6156
body = {
6257
__TYPE = "external",
6358
data = {

0 commit comments

Comments
 (0)