|
42 | 42 | -- @param bufnr Buffer number, a.k.a id |
43 | 43 | -- @param start_line Line where body starts |
44 | 44 | -- @param stop_line Line where body stops |
45 | | -local function get_body(bufnr, start_line, stop_line) |
| 45 | +-- @param has_hson True if content-type is set to json |
| 46 | +local function get_body(bufnr, start_line, stop_line, has_json) |
46 | 47 | if start_line >= stop_line then |
47 | 48 | return |
48 | 49 | end |
@@ -73,7 +74,11 @@ local function get_body(bufnr, start_line, stop_line) |
73 | 74 |
|
74 | 75 | local is_json, json_body = pcall(vim.fn.json_decode, body) |
75 | 76 | if is_json then |
76 | | - return json_body |
| 77 | + if has_json then |
| 78 | + return vim.fn.json_encode(json_body) |
| 79 | + else |
| 80 | + return json_body |
| 81 | + end |
77 | 82 | end |
78 | 83 |
|
79 | 84 | return body |
@@ -113,12 +118,10 @@ local function get_headers(bufnr, start_line, end_line) |
113 | 118 | goto continue |
114 | 119 | end |
115 | 120 |
|
116 | | - local header = utils.split(line_content, ":") |
117 | | - local header_name = header[1]:lower() |
118 | | - table.remove(header, 1) |
119 | | - local header_value = table.concat(header, ":") |
| 121 | + local header_name, header_value = line_content:match("^(.-): ?(.*)$") |
| 122 | + |
120 | 123 | if not utils.contains_comments(header_name) then |
121 | | - headers[header_name] = utils.replace_vars(header_value) |
| 124 | + headers[header_name:lower()] = utils.replace_vars(header_value) |
122 | 125 | end |
123 | 126 | ::continue:: |
124 | 127 | end |
@@ -225,7 +228,7 @@ M.get_current_request = function() |
225 | 228 |
|
226 | 229 | local curl_args, body_start = get_curl_args(bufnr, headers_end, end_line) |
227 | 230 |
|
228 | | - local body = get_body(bufnr, body_start, end_line) |
| 231 | + local body = get_body(bufnr, body_start, end_line, string.find(headers["content-type"] or "", "application/[^ ]-json")) |
229 | 232 |
|
230 | 233 | if config.get("jump_to_request") then |
231 | 234 | utils.move_cursor(bufnr, start_line) |
|
0 commit comments