Skip to content

Commit c6aed27

Browse files
authored
Merge pull request #26 from NTBBloodbath/feature/header-values-from-env
feat: be able to use environment variables in headers
2 parents 5042edf + f56b9f2 commit c6aed27

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

lua/rest-nvim/init.lua

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ local function get_body(bufnr, stop_line, query_line, json_body)
8484
)
8585

8686
for _, json_line in ipairs(json_lines) do
87-
if json_line:find('^%s+#') == nil then
87+
if not json_line:find('^%s+#') then
8888
json_string = json_string .. utils.replace_env_vars(json_line)
8989
end
9090
end
@@ -141,17 +141,19 @@ local function get_headers(bufnr, query_line)
141141
header = utils.split(header, ':')
142142
if
143143
header[1]:lower() ~= 'accept'
144-
-- If header key doesn't contains double quotes,
145-
-- so we don't get body keys
146-
and header[1]:find('"') == nil
144+
-- If header key doesn't contains double quotes at the
145+
-- start, so we don't get body keys
146+
and not header[1]:find('^%s+"')
147147
-- If header key doesn't contains hashes,
148148
-- so we don't get commented headers
149-
and header[1]:find('^%s+#') == nil
149+
and not header[1]:find('^%s+#')
150150
-- If header key doesn't contains HTTP methods,
151151
-- so we don't get the http method/url
152152
and not utils.has_value(http_methods, header[1])
153153
then
154-
headers[header[1]:lower()] = header[2]
154+
headers[header[1]:lower()] = utils.replace_env_vars(
155+
header[2]
156+
)
155157
end
156158
end
157159
end
@@ -183,7 +185,7 @@ local function get_accept(bufnr, query_line)
183185
)
184186

185187
for _, accept_data in pairs(accept_line) do
186-
if accept_data:find('^%s+#') == nil then
188+
if not accept_data:find('^%s+#') then
187189
accept = utils.split(accept_data, ':')[2]
188190
end
189191
end

tests/using_env_vars/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
URL=https://reqres.in/api/users
22
USERNAME=morpheus
3+
TOKEN=emacs-is-pinky-stop-using-it

tests/using_env_vars/post_create_user.http

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# - Body
99
POST {{URL}}
1010

11-
# Authorization: {{USER}}:{{PASSWORD}}
11+
Authorization: Bearer {{TOKEN}}
1212

1313
{
1414
"name": "{{USERNAME}}",

0 commit comments

Comments
 (0)