Problem Description
I'm trying to access environment variables defined in my .env file within post-request Lua scripts, but os.getenv() returns nil even though the environment file is properly loaded and selected.
Steps to Reproduce
-
Created a .env file with the following content:
-
Used :Rest env select command to select the env file
-
Verified the file is loaded with :Rest env show (shows correct file path)
-
Created an HTTP request with a post-script:
POST {{baseurl}}/reset
# @lang=lua
> {%
print(os.getenv("PLATFORM"))
%}
Expected Behavior
The post-script should print Prod (the value of the PLATFORM environment variable).
Actual Behavior
The post-script prints nil.
Environment
- rest.nvim version commit as per Lazy.nvim:
"rest.nvim": { "branch": "main", "commit": "de9726ab956e30202aafbcdea83c1d6bffe54227" }
NVIM v0.11.1
Build type: Release
LuaJIT 2.1.1741730670
Run "nvim -V1 -v" for more info
PRETTY_NAME="Ubuntu 24.04.2 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.2 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
Questions
- Is
os.getenv() the correct way to access environment variables from the .env file in post-scripts?
- Are environment variables from the
.env file supposed to be available in the Lua execution context?
- Is there a different API or method to access these variables in post-scripts?
- How can I debug what variables are available in the post-script execution context?
Additional Context
- The
{{baseurl}} variable substitution works correctly in the request itself
- The
:Rest env show command confirms the environment file is loaded
Any guidance on the proper way to access environment variables in post-request scripts would be greatly appreciated!
Originally posted by @maniac-en in #549
Problem Description
I'm trying to access environment variables defined in my
.envfile within post-request Lua scripts, butos.getenv()returnsnileven though the environment file is properly loaded and selected.Steps to Reproduce
Created a
.envfile with the following content:Used
:Rest env selectcommand to select the env fileVerified the file is loaded with
:Rest env show(shows correct file path)Created an HTTP request with a post-script:
Expected Behavior
The post-script should print
Prod(the value of the PLATFORM environment variable).Actual Behavior
The post-script prints
nil.Environment
Questions
os.getenv()the correct way to access environment variables from the.envfile in post-scripts?.envfile supposed to be available in the Lua execution context?Additional Context
{{baseurl}}variable substitution works correctly in the request itself:Rest env showcommand confirms the environment file is loadedAny guidance on the proper way to access environment variables in post-request scripts would be greatly appreciated!
Originally posted by @maniac-en in #549