How to interate over all Postman environment variables? - request

In one of my pre-request script I need to have my url with all environment variables reaplced. Suddenly the env vars are injected only after the pre-request script. I want to iterate over the env variables and manually replace them. Is it possible?
I can get pm.environment.values, but suddenly this object is not an array. I cant get any values from it with pm.environment.values[0] or use a for(const element of pm.environment.values) on it.
If I could get all environment keys, i could acomlish my aim with pm.environment.get, but I did not found a way to do it.

You can use the .toObject() function. It returns all variables with their values, in the active environment, in a single object:
pm.environment.toObject()
https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-environment-variables-in-scripts
This will also work for other variable scopes such as Collection, Iteration and Global.

Related

Issue while trying to initialize the react property

Issue details
.env value not initialized in the react property
siteKey value is always blank
Property in react
const [siteKey] = useState(process.env.REACT_CAPTCHA_SITE_KEY);
Key in .env
REACT_CAPTCHA_SITE_KEY='some key'
Html
<ReCAPTCHA sitekey={siteKey}/>
Main issue is with your environment declaration, as per create-react-app:
You must create custom environment variables beginning with
REACT_APP_. Any other variables except NODE_ENV will be ignored to
avoid accidentally exposing a private key on the machine that could
have the same name. Changing any environment variables will require
you to restart the development server if it is running.
As you don't have any setter method needed for useState, you can define your variable just with const, like:
const siteKey = process.env.REACT_APP_CAPTCHA_SITE_KEY
I saw Some Issue in your code, First thing is,UseState hook initialization is wrong.
It Should be,
const [siteKey, setsiteKey] = useState(process.env.REACT_APP_CAPTCHA_SITE_KEY);
And also You don't need to use this environment variable as useState, because it is not changing so, when you need to access environment variable, use as below,
ex:const siteKey=process.env.REACT_APP_CAPTCHA_SITE_KEY;
Other Important Issues are,
Your environment variable should prefix with the REACT_APP_ in the .env file.
ex:process.env.REACT_APP_SOME_VARIABLE
And possible Issue can be, your .env file is not in your root which is that, within the same directory as the package. json file.
Key in .env should be like as shown below
REACT_APP_CAPTCHA_SITE_KEY=some_key_value
Also whenever you edit your .env file you need to restart your React app to take effect of your modified file.

Exposing Next.js Environment Variables to PWA's API-Platform

According to the official documentation of Next.js, to expose an environment variable is necessary to use NEXT_PUBLIC_ prefix, but the admin uses process.env.REACT_APP_API_ENTRYPOINT.
In my case to access the REACT_APP_API_ENTRYPOINT env var, I needed to substitute to process.env.NEXT_PUBLIC_REACT_APP_API_ENTRYPOINT. Only with this pwa can access the value.
Is that right or am I making a mistake in changing this value?
You are right partially, Next exposes env variables that has a prefix NEXT_PUBLIC_ automatically, if your app expects to get REACT_APP_API_ENTRYPOINT, there is no point of renaming the variable, since your app won't consume it.
If I understood your scenario correctly, you need to expose REACT_APP_API_ENTRYPOINT, since next doesn't do it automatically, you need to specify if manually in your next.config.js file.
//next.config.js
module.exports = {
...
env: {
REACT_APP_API_ENTRYPOINT: process.env.REACT_APP_API_ENTRYPOINT, // assumes that your variable is defined
}
...
}

update source code of react through azure pipeline

I have created a react environment variable in .env file and is able to update it through command prompt or power shell now my aim is to update it through azure pipeline so add a powershell script.But I am not able to figure out how to read or write data in .env file through a azure pipeline powershell script. I wish to change base url for differnet envirnoments dev stage prod (base_url_dev = https://projectName.dev.azurewebsites.net/). Please let me know If there is any other way
You would commit your file like this:
base_url = '#{base_url}#' (never commit enivornment specific values into Source Code)
And then set a base_url variable in each AzDo Stage to the actual value. (You can also use stage scoped variable groups)
in each stage, use a replacetokens step and target your .env file to replace the #{base_url}# to the actual value you declared for each stage.
update source code of react through azure pipeline
We could remove the base url from the .env file, then use a Inline powershell task to set the base url via Logging Command:
Write-Host "##vso[task.setvariable variable=REACT_BASE_URL;]$(REACT_BASE_URL)"
Then define the variable REACT_BASE_URL with different value for different stages in the Variables tab:
Alternatively, just like michiel said, we could use the task Replace Tokens to update the value in the .env file.
Change the base_urls values to #{REACT_BASE_URL}#`:
REACT_BASE_URL = #{REACT_BASE_URL}#
Then, also defined the variable REACT_BASE_URL with different value for different stages in the Variables tab.
And add the task Replace Tokens in the pipeline:

Looping with multiple variables in a URL using Postman

Using postman tool, I am trying to loop in multiple values in the place of a specific parameter 'memid' in the sample URL https://www.testdomain.com/login?memid=123
I have replaced the value '123' as {{id}} in the URL and then declared a single value '123' in Globals. It's working fine when I execute this. But, is there a way where I can loop the URL and replace the variable memid every time with some other predefined unique values? Should I be storing all the values in a separate csv file?? Thanks in advance.
You could take a look at using a Data file in the Collection Runner to help with this - More information can be found here.
https://learning.getpostman.com/docs/postman/collection_runs/working_with_data_files/

Passing variable in XML topology in ODI

I am trying to parameterized the topology connection in ODI to load multiple xml of same structure one by one using variable.But i am getting unknown token error.
Jdbc url :jdbc:snps:xml?f=U:/SOTI_CLOUD/#B.xml
{ #B is ODI variable having file name)
Try using #GLOBAL.B if it is a Global variable or #<PROJECT_NAME>.B if it is a project variable.
Also check what is your history setting for that variable. If it is set on "No History", make sure you are declaring/refreshing the variable within the same session in which you want to access that XML file.
Just a hunch…
For the variable to be picked up in JDBC URL you need to launch a separate scenario. Your problem might be different but make sure you have an outer loop with variable declare/refresh and whenever you refresh/increment it you launch a separate scenario (not just an interface) where you load data using such constructed URL.

Resources