I want to use thunderclient to test an http api.
The steps below are all working
+--------+ +------------------+
| |--1 -- Login with user + pass -->| Server |
| | | |
| |<-2 -- Login reply with access_token ---| if user+pass ok |
| | | |
| |--3 -- checkConfig-request with token -->| check token |
| Client | | |
| |<-4 -- Reply with payload ---| if token ok |
+--------+ +------------------+
But currently i copy the value of access_token
from step 2 into step 3 and then send the request manually.
This is the reply from step 2
{
"access_token": "---best---kept---secret---",
"expires_in": 300,
"refresh_expires_in": 0,
"token_type": "Bearer",
"not-before-policy": 1626860494,
"scope": ""
}
I want to use the value from access_token
from the Login
-Reply (2) pass it to a variable {{access_token_cc}}
and then use this variable to pass the value of access_token
as the Bearer Token
in the checkConfig
-request (3) (see screen)
Question – how to use a variable?
I do not quite understand how to create a variable and assign a value to it. There is a video on youtube but the step to put the pieces together is to brief and not really explained.
2
Answers
rioV8 did point out from the documentation how to "connect / wire up" a variable to a value in the reply. Because the docs do not contain any images the screenshots below hopefully help to illustrate how to set up variables.
Example to create and assign a variable
To be able to use a variable three things must be done
Env
, create a New Environment. Inside it create a variablefruitShop.next_url
and optionally assign an initial value. Set the Environment to active.json.access_token = {{access_token_cc}}
The fruitShop-REST-API (german) reply returns a json structure like this
Create
In the screen you can see
fruitShop
fruitShop.next_url
andfruitShop.count
; both with initial values.Assign
To assign a value to the variable you need to switch to the tab collections
fruitShop
) andGet Products 1
)Get Products 1
) and switch to the tabtest
.With the option
Set Environment Variable
you can assign a response value to a variable.In the screen the response fields (
json.meta.count
andjson.meta.next_url
) are assigned to variables from your envirionment:Use
You can now use the variable with this syntax
{{yourVariable}}
According to the documentation
Set the source of the variable to
json.access_token
Determine which type of environment you want to store it to