@@ -21,7 +21,7 @@ local function get_importfile_name(bufnr, start_line, stop_line)
2121 local fileimport_spliced
2222 fileimport_line = vim .api .nvim_buf_get_lines (bufnr , import_line - 1 , import_line , false )
2323 fileimport_string =
24- string.gsub (fileimport_line [1 ], " <" , " " , 1 ):gsub (" ^%s+" , " " ):gsub (" %s+$" , " " )
24+ string.gsub (fileimport_line [1 ], " <" , " " , 1 ):gsub (" ^%s+" , " " ):gsub (" %s+$" , " " )
2525 fileimport_spliced = utils .replace_vars (fileimport_string )
2626 if path :new (fileimport_spliced ):is_absolute () then
2727 return fileimport_spliced
@@ -56,14 +56,19 @@ local function get_body(bufnr, start_line, stop_line, has_json)
5656 end
5757
5858 local body = " "
59+ local vars = utils .read_variables ()
5960 -- nvim_buf_get_lines is zero based and end-exclusive
6061 -- but start_line and stop_line are one-based and inclusive
6162 -- magically, this fits :-) start_line is the CRLF between header and body
6263 -- which should not be included in the body, stop_line is the last line of the body
6364 for _ , line in ipairs (lines ) do
65+ -- stop if a script opening tag is found
66+ if line :find (" {%%" ) then
67+ break
68+ end
6469 -- Ignore commented lines with and without indent
6570 if not utils .contains_comments (line ) then
66- body = body .. utils .replace_vars (line )
71+ body = body .. utils .replace_vars (line , vars )
6772 end
6873 end
6974
@@ -80,13 +85,46 @@ local function get_body(bufnr, start_line, stop_line, has_json)
8085 json_body [key ] = vim .fn .json_encode (val )
8186 end
8287 end
83- return json_body
88+ return vim . fn . json_encode ( json_body )
8489 end
8590 end
8691
92+
8793 return body
8894end
8995
96+ local function get_response_script (bufnr , start_line , stop_line )
97+ local all_lines = vim .api .nvim_buf_get_lines (bufnr , start_line , stop_line , false )
98+ -- Check if there is a script
99+ local script_start_rel
100+ for i , line in ipairs (all_lines ) do
101+ -- stop if a script opening tag is found
102+ if line :find (" {%%" ) then
103+ script_start_rel = i
104+ break
105+ end
106+ end
107+
108+ if script_start_rel == nil then
109+ return nil
110+ end
111+
112+ -- Convert the relative script line number to the line number of the buffer
113+ local script_start = start_line + script_start_rel - 1
114+
115+ local script_lines = vim .api .nvim_buf_get_lines (bufnr , script_start , stop_line , false )
116+ local script_str = " "
117+
118+ for _ , line in ipairs (script_lines ) do
119+ script_str = script_str .. line .. " \n "
120+ if line :find (" %%}" ) then
121+ break
122+ end
123+ end
124+
125+ return script_str :match (" {%%(.-)%%}" )
126+ end
127+
90128-- is_request_line checks if the given line is a http request line according to RFC 2616
91129local function is_request_line (line )
92130 local http_methods = { " GET" , " POST" , " PUT" , " PATCH" , " DELETE" }
@@ -202,7 +240,8 @@ local function end_request(bufnr, linenumber)
202240 end
203241 utils .move_cursor (bufnr , linenumber )
204242
205- local next = vim .fn .search (" ^GET\\ |^POST\\ |^PUT\\ |^PATCH\\ |^DELETE" , " cn" , vim .fn .line (" $" ))
243+ local next = vim .fn .search (" ^GET\\ |^POST\\ |^PUT\\ |^PATCH\\ |^DELETE\\ ^###\\ " , " cn" ,
244+ vim .fn .line (" $" ))
206245
207246 -- restore cursor position
208247 utils .move_cursor (bufnr , oldlinenumber )
@@ -296,24 +335,28 @@ M.buf_get_request = function(bufnr, curpos)
296335 content_type :find (" application/[^ ]*json" )
297336 )
298337
338+ local script_str = get_response_script (bufnr , headers_end , end_line )
339+
340+
299341 if config .get (" jump_to_request" ) then
300342 utils .move_cursor (bufnr , start_line )
301343 else
302344 utils .move_cursor (bufnr , curpos [2 ], curpos [3 ])
303345 end
304346
305347 return true ,
306- {
307- method = parsed_url .method ,
308- url = parsed_url .url ,
309- http_version = parsed_url .http_version ,
310- headers = headers ,
311- raw = curl_args ,
312- body = body ,
313- bufnr = bufnr ,
314- start_line = start_line ,
315- end_line = end_line ,
316- }
348+ {
349+ method = parsed_url .method ,
350+ url = parsed_url .url ,
351+ http_version = parsed_url .http_version ,
352+ headers = headers ,
353+ raw = curl_args ,
354+ body = body ,
355+ bufnr = bufnr ,
356+ start_line = start_line ,
357+ end_line = end_line ,
358+ script_str = script_str
359+ }
317360end
318361
319362M .print_request = function (req )
0 commit comments