Highlighted
Valued Contributor.
Valued Contributor.
5231 views

Parsing JSON to get Access_token from response of an HTTP service

Jump to solution

Hi, I need to pass my access_token to my service which is in response(JSON) of my token service, I have tried usign web_reg_save_param_ex to get my token but no luck,

web_reg_save_param_ex( "ParamName=access_token", "LB=", "RB=", SEARCH_FILTERS, LAST);

attached is format of my JSON response, Please help.

Tags (1)
0 Likes
1 Solution

Accepted Solutions
Highlighted
Valued Contributor.
Valued Contributor.

Re: Parsing JSON to get Access_token from response of an HTTP service

Jump to solution

Thanks,

will give a try to it. issues is also resolved using a simple loadrunner function 

web_reg_save_param_json();

used it as following before my token service to get access token 

web_reg_save_param_json(

       "ParamName=CorrelationParameter",

       "QueryString=$.access_token",

       SEARCH_FILTERS,

       "Scope=Body",

       LAST);

later passed it using web_auto_set_header()  to all subsequent API calls.

View solution in original post

0 Likes
6 Replies
Highlighted
Respected Contributor.
Respected Contributor.

Re: Parsing JSON to get Access_token from response of an HTTP service

Jump to solution

Please post ur code here, I don't want to download any file

0 Likes
Highlighted
Valued Contributor.
Valued Contributor.

Re: Parsing JSON to get Access_token from response of an HTTP service

Jump to solution

Here is JSON response 

 

{
"access_token": "Sq22OiGq94......................",
"token_type": "bearer",
"expires_in": 1499,
"refresh_token": "a12..................",
"as:client_id": "",
"userName": "...",
"firstName": "...",
"lastName": "..",
"privileges": "{\"......."}",
".issued": "Mon, 30 Oct 2017 05:03:50 GMT",
".expires": "Mon, 30 Oct 2017 05:28:50 GMT"
}

0 Likes
Highlighted
Super Contributor.
Super Contributor.

Re: Parsing JSON to get Access_token from response of an HTTP service

Jump to solution

Hello,

If you need to corelate the dynamic values from json, you need to write the custom java script code, please follow below mentioned steps.

Step1: Take the whole response of specified request by using web_reg_save_param("Response1","LB=","RB=","Search=Body",LAST);

Step2: Write javascript function after creating the external javascript sheet (json.js) to retrive the values from json response

function getCompleteData(Responsedata)
{
data = JSON.parse(Responsedata);
var Responsedata1 = data.d; //this is the json path of dynamic value
return Responsedata1;
}

Step 3: Put the below mentioned javascript in next to the specified request in Action.c file

web_js_run("Code=getCompleteData(LR.getParam('SessionResponse1'));",
"ResultParam=FinalData",
SOURCES,
"File=json.js",
ENDITEM,
LAST);

Step 4: Remove the \\ slahes with the help of below code

lr_replace("FinalData", "\\r\\n", "");
lr_replace("FinalData", "\\\\\\\"", "\"");
lr_replace("FinalData", "\\\"", "\"");
lr_replace("FinalData", "\/\"", "\"");
lr_replace("FinalData", "\"\/", "\"");
lr_replace("FinalData", "\"\{", "\{");
lr_replace("FinalData", "\}\"", "\}");

Step 5: Put the menotioned code in global.h file to use lr_replace function

char *strReplace(const char *src, const char *from, const char *to)
{
char *value;
char *dst;
char *match;
int size;
int fromlen;
int tolen;

size = strlen(src) + 1;
fromlen = strlen(from);
tolen = strlen(to);

value = (char *)malloc(size);

dst = value;

if ( value != NULL )
{
for ( ;; )
{
match = (char *) strstr(src, from);
if ( match != NULL )
{
size_t count = match - src;
char *temp;
size += tolen - fromlen;
temp = (char *)realloc(value, size);

if ( temp == NULL )
{
free(value);
return NULL;
}

dst = temp + (dst - value);
value = temp;

memmove(dst, src, count);
src += count;
dst += count;

memmove(dst, to, tolen);
src += fromlen;
dst += tolen;
}
else
{
strcpy(dst, src);
break;
}
}
}
return value;
}

int lr_replace( const char *lrparam, char *findstr, char *replacestr )
{
int res = 0;
char *result_str;
char lrp[1024];

sprintf( lrp, "{%s}", lrparam);

result_str = strReplace( lr_eval_string(lrp), findstr, replacestr );

if (result_str != NULL )
{
lr_save_string( result_str, lrparam );
free( result_str );
res = 1;
}
return res;
}

Step 6: After replacing the \\ slahes pass the below mentioned web_reg_save_param function

web_js_run("Code=getSessionDataID(LR.getParam('CompleteData'));",
"ResultParam=SessionID",
SOURCES,
"File=json.js",
ENDITEM,
LAST);

Step 7: Pass the SessionID where ever you want to pass the token in script

Regards,

Manoj Thota

 

 

Highlighted
Valued Contributor.
Valued Contributor.

Re: Parsing JSON to get Access_token from response of an HTTP service

Jump to solution

Thanks for detailed reply Manoj

I was getting an unexpected syntax error which was resolved by chagning parameter name (for getCompleteDate function Response1 should be used). Now i am having two issues

1- when i run code step by step all steps to elimiate slashes reutrn Final Data as undefined.

2- when my script reached at following step

web_js_run("Code=getSessionDataID(LR.getParam('CompleteData'));",

it failed with below error

Action.c(245): Error -26000: Error from JS Engine: ":0:ReferenceError: getSessionDataID is not defined
" [MsgId: MERR-26000]

0 Likes
Highlighted
Super Contributor.
Super Contributor.

Re: Parsing JSON to get Access_token from response of an HTTP service

Jump to solution

Hello Sameen,

Its just typo error please remove the step 6 and step 7 and pass the FinalData value where ever you want to pass the token in script.

If the above approach is working, please accept the solution.

Regards,

Manoj Thota

0 Likes
Highlighted
Valued Contributor.
Valued Contributor.

Re: Parsing JSON to get Access_token from response of an HTTP service

Jump to solution

Thanks,

will give a try to it. issues is also resolved using a simple loadrunner function 

web_reg_save_param_json();

used it as following before my token service to get access token 

web_reg_save_param_json(

       "ParamName=CorrelationParameter",

       "QueryString=$.access_token",

       SEARCH_FILTERS,

       "Scope=Body",

       LAST);

later passed it using web_auto_set_header()  to all subsequent API calls.

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.