Skip to content

Commit f7adfc4

Browse files
authored
fix(curl): stream option handling in curl (#557)
There are two major problems when supplying `opts.stream` 1. curl methods do produce junk in temp folder. Curl command dumps headers to some file in tmp folder and cleans it up only in the `parse.response()` method. This method is not called if `opts.stream` is set 2. there is no way to access response code, because it is also writen to the headers dump file in temp folder. This commit address both of those problems by setting `job.on_exit` callback even if `opts.stream` is specified.
1 parent 6778384 commit f7adfc4

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

lua/plenary/curl.lua

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -280,30 +280,31 @@ local request = function(specs)
280280
command = "curl",
281281
args = args,
282282
}
283+
283284
if opts.stream then
284285
job_opts.on_stdout = opts.stream
285-
else
286-
job_opts.on_exit = function(j, code)
287-
if code ~= 0 then
288-
local stderr = vim.inspect(j:stderr_result())
289-
local message = string.format("%s %s - curl error exit_code=%s stderr=%s", opts.method, opts.url, code, stderr)
290-
if opts.on_error then
291-
return opts.on_error {
292-
message = message,
293-
stderr = stderr,
294-
exit = code,
295-
}
296-
else
297-
error(message)
298-
end
299-
end
300-
local output = parse.response(j:result(), opts.dump[2], code)
301-
if opts.callback then
302-
return opts.callback(output)
286+
end
287+
288+
job_opts.on_exit = function(j, code)
289+
if code ~= 0 then
290+
local stderr = vim.inspect(j:stderr_result())
291+
local message = string.format("%s %s - curl error exit_code=%s stderr=%s", opts.method, opts.url, code, stderr)
292+
if opts.on_error then
293+
return opts.on_error {
294+
message = message,
295+
stderr = stderr,
296+
exit = code,
297+
}
303298
else
304-
response = output
299+
error(message)
305300
end
306301
end
302+
local output = parse.response(j:result(), opts.dump[2], code)
303+
if opts.callback then
304+
return opts.callback(output)
305+
else
306+
response = output
307+
end
307308
end
308309
local job = J:new(job_opts)
309310

0 commit comments

Comments
 (0)