Skip to content

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

@maniac-en

Description

@maniac-en

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:

    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:
NVIM v0.11.1
Build type: Release
LuaJIT 2.1.1741730670
Run "nvim -V1 -v" for more info
  • OS:
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 #549

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions