skip to Main Content

I want to use 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)

Thunderclient_how_to_pass_a_variable

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


  1. Chosen as BEST ANSWER

    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

    1. Create: Create a variable as part of an environment. Switch to tab Env, create a New Environment. Inside it create a variable fruitShop.next_url and optionally assign an initial value. Set the Environment to active.
    2. Assign: Switch to tab collection, create a new collection, add a request. For a request under the tab test the value of a response field can be assigned to a variable - see screen below json.access_token = {{access_token_cc}}
    3. Use: In a request you can use a variable from your active Environment with the syntax {{yourvariable}}.

    The fruitShop-REST-API (german) reply returns a json structure like this

    {
      "meta": { "count": 32, "limit": 10, "page": 1
              ,"next_url": "/shop/products/?page=2&limit=10" }
      , "products":[ 
          { "name": "Bananas", "product_url": "/shop/products/3"}
          , { ... }]
    }
    

    Create

    In the screen you can see

    • the environment fruitShop
    • and some variables - for example fruitShop.next_url and fruitShop.count; both with initial values.

    Thunderclient create a variable

    Assign

    To assign a value to the variable you need to switch to the tab collections

    • Create a collection (here fruitShop) and
    • Add a request (here Get Products 1)
    • Click the request (here Get Products 1) and switch to the tab test.

    Thunderclient assign value to var

    With the option Set Environment Variable you can assign a response value to a variable.

    Thunderclient Request Test set environment variable

    In the screen the response fields (json.meta.count and json.meta.next_url) are assigned to variables from your envirionment:

    // response field       environment variable
    json.meta.count = {{fruitShop.count}}
    json.meta.next_url = {{fruitShop.next_url}}
    

    Use

    You can now use the variable with this syntax {{yourVariable}}

    Thunderclient use a  variable


  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search