7171-- get_importfile returns in case of an imported file the absolute filename
7272-- @param bufnr Buffer number, a.k.a id
7373-- @param stop_line Line to stop searching
74- local function get_importfile (bufnr , stop_line )
75- local import_line = 0
76- import_line = vim .fn .search (' ^<' , ' n' , stop_line )
74+ local function get_importfile (bufnr ,start_line , stop_line )
75+ -- store old cursor position
76+ local oldpos = vim .fn .getcurpos ()
77+ go_to_line (bufnr , start_line )
78+
79+ local import_line = vim .fn .search (' ^<' , ' n' , stop_line )
80+ -- restore old cursor position
81+ go_to_line (bufnr , oldpos [2 ])
82+
7783 if import_line > 0 then
7884 local fileimport_string = ' '
7985 local fileimport_line = {}
@@ -102,53 +108,40 @@ end
102108-- either a raw string with the body if it is JSON, or a filename. Plenary.curl can distinguish
103109-- between strings with filenames and strings with the raw body
104110-- @param bufnr Buffer number, a.k.a id
105- -- @param stop_line Line to stop searching
106- -- @param json_body If the body is a JSON formatted POST request, false by default
107- local function get_body (bufnr , stop_line , json_body )
108- -- store old cursor position
109- local oldpos = vim .fn .getcurpos ()
110- if not json_body then
111- json_body = false
112- end
113- local json = nil
114- local start_line = 0
115- local end_line = 0
111+ -- @param start_line Line where body starts
112+ -- @param stop_line Line where body stops
113+ local function get_body (bufnr , start_line , stop_line )
114+ if start_line >= stop_line then
115+ return
116+ end
116117
117118 -- first check if the body should be imported from an external file
118- local importfile = get_importfile (bufnr , stop_line )
119+ local importfile = get_importfile (bufnr , start_line , stop_line )
119120 if importfile ~= nil then
120121 return importfile
121122 end
122123
123- if json_body then
124- start_line = vim .fn .search (' ^{' , ' ' , stop_line )
125- end_line = vim .fn .searchpair (' {' , ' ' , ' }' , ' n' , ' ' , stop_line )
126-
127- if start_line > 0 then
128- local json_string = ' '
129- local json_lines = {}
130- json_lines = vim .api .nvim_buf_get_lines (
131- bufnr ,
132- start_line ,
133- end_line - 1 ,
134- false
135- )
136-
137- for _ , json_line in ipairs (json_lines ) do
138- -- Ignore commented lines with and without indent
139- if not utils .contains_comments (json_line ) then
140- json_string = json_string .. utils .replace_vars (json_line )
141- end
142- end
143-
144- json = ' {' .. json_string .. ' }'
145- end
146-
147- -- restore old cursor position
148- go_to_line (bufnr , oldpos [2 ])
149-
150- return json
151- end
124+ local lines = {}
125+ local body = " "
126+ -- nvim_buf_get_lines is zero based and end-exclusive
127+ -- but start_line and stop_line are one-based and inclusive
128+ -- magically, this fits :-) start_line is the CRLF between header and body
129+ -- which should not be included in the body, stop_line is the last line of the body
130+ lines = vim .api .nvim_buf_get_lines (
131+ bufnr ,
132+ start_line ,
133+ stop_line ,
134+ false
135+ )
136+ for _ , line in ipairs (lines ) do
137+ -- Ignore commented lines with and without indent
138+ if not utils .contains_comments (line ) then
139+ body = body
140+ .. utils .replace_vars (line )
141+ end
142+ end
143+
144+ return body
152145end
153146
154147-- is_request_line checks if the given line is a http request line according to RFC 2616
@@ -169,6 +162,7 @@ local function get_headers(bufnr, query_line)
169162 local headers = {}
170163 -- Set stop at end of buffer
171164 local stop_line = vim .fn .line (' $' )
165+ local body_start = 0
172166
173167 -- Iterate over all buffer lines
174168 for line_number = query_line + 1 , stop_line do
@@ -178,6 +172,7 @@ local function get_headers(bufnr, query_line)
178172 -- message header and message body are seperated by CRLF (see RFC 2616)
179173 -- for our purpose also the next request line will stop the header search
180174 if is_request_line (line_content ) or line_content == ' ' then
175+ body_start = line_number
181176 break
182177 end
183178 if not line_content :find (' :' ) then
@@ -195,7 +190,7 @@ local function get_headers(bufnr, query_line)
195190 :: continue::
196191 end
197192
198- return headers
193+ return headers , body_start
199194end
200195
201196-- curl_cmd runs curl with the passed options, gets or creates a new buffer
296291local function end_request (bufnr )
297292 -- store old cursor position
298293 local curpos = vim .fn .getcurpos ()
299- local linenumber = curpos [1 ]
294+ local linenumber = curpos [2 ]
300295 local oldlinenumber = linenumber
301296
302297 -- start searching for next request from the next line
@@ -333,20 +328,9 @@ rest.run = function(verbose)
333328
334329 local parsed_url = parse_url (vim .fn .getline (start_line ))
335330
336- local headers = get_headers (bufnr , start_line )
337-
338- local body
339- -- If the header Content-Type was passed and it's application/json then return
340- -- body as `-d '{"foo":"bar"}'`
341- if
342- headers ~= nil
343- and headers [' content-type' ] ~= nil
344- and string.find (headers [' content-type' ], ' application/json' )
345- then
346- body = get_body (bufnr , end_line , true )
347- else
348- body = get_body (bufnr , end_line )
349- end
331+ local headers , body_start = get_headers (bufnr , start_line )
332+
333+ local body = get_body (bufnr , body_start , end_line )
350334
351335 local success_req , req_err = pcall (curl_cmd , {
352336 method = parsed_url .method :lower (),
0 commit comments