ITADN

How to access environment variables in post-request scripts (Lua)?

#550Openmaniac-en 创建于 2025-06-26
M
maniac-encommented
### 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 1. Created a `.env` file with the following content: ``` PLATFORM=Prod ``` 2. Used `:Rest env select` command to select the env file 3. Verified the file is loaded with `:Rest env show` (shows correct file path) 4. Created an HTTP request with a post-script: ```http 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" } ``` - **Neovim version**: ```sh NVIM v0.11.1 Build type: Release LuaJIT 2.1.1741730670 Run "nvim -V1 -v" for more info ``` - **OS**: ```sh 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 1. Is `os.getenv()` the correct way to access environment variables from the `.env` file in post-scripts? 2. Are environment variables from the `.env` file supposed to be available in the Lua execution context? 3. Is there a different API or method to access these variables in post-scripts? 4. 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 https://github.com/rest-nvim/rest.nvim/discussions/549_
2 条评论