简体   繁体   中英

Loadrunner, how to get cookie value?

After authorizing in my website, it creates special value in Cookies:

Cookies: session=bb415f62-eb9e-42fa-aedf-ca1887548216

The value of this cookie is required for other API calls on my website. My autogenerated script created this:

web_add_header("token", 
    "bb415f62-eb9e-42fa-aedf-ca1887548216");

However, there is a problem. Value of Cookie session constantly changes after each authorization. This web_add_header has constant value, which is expired.

Is it possible to get value from Cookie and set it to header in loadrunner?

Please check the following example for Web HTTP protocol:

You will need something like this to extract cookie value:

//  Set-Cookie: session=bb415f62-eb9e-42fa-aedf-ca1887548216; path=/
    web_reg_save_param_ex(
        "ParamName=session",
        "LB=session=",
        "RB=;",
        "Ordinal=all",
        "Notfound=warning",
        SEARCH_FILTERS,
        "Scope=Cookies",  
        LAST);

then use it like this:

web_add_header("token", "{session}");

Here is a runnable example:


   //Extract the uuid and save it under "session" parameter
    web_reg_save_param_ex(
        "ParamName=session",
        "LB=\"uuid\": \"",
        "RB=\"",
        "Ordinal=all",
        "Notfound=warning",
        SEARCH_FILTERS,
        "Scope=Body", 
        LAST);

    web_url("get data",
            "URL=http://httpbin.org/uuid",
            "Snapshot=t1.inf",
            LAST);


    if (atoi(lr_eval_string("{session_count}"))>0) lr_save_string(lr_eval_string("{session_1}"),"token");

    web_add_header("token", "{token}");

    web_reg_save_param_ex(
        "ParamName=session",
        "LB=\"uuid\": \"",
        "RB=\"",
        "Ordinal=all",
        "Notfound=warning",
        SEARCH_FILTERS,
        "Scope=Body",
        LAST);

    web_url("get data_2",
            "URL=http://httpbin.org/uuid",
            "Snapshot=t2.inf",
            LAST);

//If there is a new session, overwrite the token parameter
    if (atoi(lr_eval_string("{session_count}"))>0) lr_save_string(lr_eval_string("{session_1}"),"token");

    web_add_header("token", "{token}");

    web_reg_save_param_ex(
        "ParamName=session",
        "LB=\"uuid\": \"",
        "RB=\"",
        "Ordinal=all",
        "Notfound=warning",
        SEARCH_FILTERS,
        "Scope=Body",
        LAST);

    web_url("get data_3",
            "URL=http://httpbin.org/uuid",
            "Snapshot=t3.inf",
            LAST);   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM