question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Import variables from another file

See original GitHub issue

Background

When we develop and test some projects, we need to perform certain HTTP requests which are restricted behind authentication, where a token should be included as a header or a search parameter. Also, different testers in a project may have their respective accounts, where each one has its distinguished username and token.

Comparison of Existing Possible Solutions

Solution 1: Use a .env file

It can help, but there is a compatibility problem.

Other implementations in other editors may or may not support the syntax to cite dotenv variables.

Generally, importing variables as plain variables should be compatible enough.

Solution 2: Extension-wide variables

This would require the .http file to cite the variable with a unique name, where the name of a variable must not collide with the variables in other projects which coexist with the current project on the same machine.

However, sometimes we need to work with some similarly structured projects, which honor the same convention on variable naming, resulting in reusing the same variable name across projects. This case is especially prominent for enterprise IT solution providers (teams and freelancers).

Major Concerns

Our major concerns are listed as:

  • Reuse local variable syntax;
  • Avoid duplicate definitions across multiple .http files;
  • Stay compatible with existing declarations, if possible.

Proposed Solution

Declaring Variables

The variables are defined in .env like before. There shall be no difference for people who already practice Solution 1.

Alternatively, the user may choose to use a different file; but the syntax in the file stays compatible with the .env file.

Using Variables

In a .http file, the user may write a line:

from .env import authToken

… to import the authToken variable. After imported, a request can cite authToken variable with {{authToken}}; the syntax is same to citing a locally defined variable.

Also, the user may write a line:

from .env import { authToken, username, hostAndPort}

… to import 3 variables in 1 line.

Therefore, the user may write a request like:

GET http://{{hostAndPort}}/api/userProfile/{{username}}
Bearer: {{username}}:{{authToken}}

If the user chooses to use a different file, the .env substring in the examples above shall be replaced with the actual filename.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:6
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
Sebaestschjincommented, Feb 7, 2022

Is it also possible to “store” a variable into the environment?

My usecase is, that I have different types of login requests for the API. All result in a OAuth Bearer Token. The actual API calls then use that token and may retrieve different results depending on which login call was used.

I’d like to have one file per API resource and separate the login requests into different files. My goal would be, to store the result of a login request into a variable and simply use that in the header of the others request. Otherwise I’d have to duplicate the login calls into all API resource files, which is quite nasty.

6reactions
adibradfieldcommented, May 22, 2021

Currently, you can store variables under the vscode setting rest-client.environment Variables and access them as standard variables. Because this is a vscode setting, you can set values for your user, machine, or the workspace. Just go to the preferences in vscode and search for rest-client.environmentVariables. You will need to manually enter your variables as a json dictioonary into the settings file.

This means that you can put variables that you want to share between projects in either your user or machine settings, and then any project specific variables can go in your workspace settings.

As well as having the 3 places you can put these variables, you can also have different environments like this (the $shared environment gets shared between all environments):

{      
    "rest-client.environmentVariables": {

        "$shared": {
          "timeout": "60000"
        },
        "development": {
          "url": "dev.example.com"
        },
        "staging": {
          "url": "stage.example.com"
        },
        "production": {
          "url": "example.com"
        }
      }
}

You can then switch environments by opening the command pallete and running the REST Client: Switch Environment command

image

Does this feature solve your problem?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I call a variable from one feature file to another ...
I have stored the id in 'newId' variable. How can I use this variable and its value in another feature file? karate.
Read more >
Import/Export variable lists - SmartBear Community
We would like to be able to share variables lists from all levels without having to share project files or add them all...
Read more >
How to call a variable/pass a parameter from one feature file ...
karate #apiauotmaitonThis video explain how to pass variables from one feature file to another feature.
Read more >
Importing variables from one syntax to another - Sublime Forum
Thanks (still, a built-in functionality for it would be neat): ... like hundreds of literals which need to be updated in like twenty...
Read more >
Import variable from a file - Feature Requests (read-only)
I see in the RPA Express has a function called “Import variable from .csv file”. May I ask that function is able to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found