Need help with URL tags or parameters
Hi There,
Hope you are doing well..
I am developing a virtual service in Parasoft. I have a requirement to use URL tags or parameters and build the response based the values in the tag values.
Sample URL is http://servername:port/valx/cust/{"c-id":"1970840"}/acdtls/{"acno":"11909262","code":"GXIC200088","e-id":80,"zone":"P","exnt-no":"PP00881SS19703"}/txdtls/{"st-dt":"2020-10-30","end-dt":"2022-10-30","p-ind":M,"ttype":"X3","from-range":0,"to-range":119}/
In generic format valx/cust/{parm1}/acdtls/{parm2}/txdtls/{parm3}/
Please help me with below points
- Should I configure full URL under "Virtual Asset Deployments Settings" like below
Or just like below?
Can I use REST URL Data Bank for my scenario? How can get the URL tag or parameters using REST URL Data Bank in my scenario?
I need to extract c-id, acno, st-dt and end-dt values into my script to build the response.
Kindly assist me in this regard.
Thanks,
Sati
Best Answers
-
Hi There,
I tried using JSON Data Bank but unable to get/refer the variables that are saved using REST URL Data Bank. So, I have used "Scripted" option Data Generator Tool.
Script:
import com.parasoft.api.* import groovy.json.JsonSlurper def script(context) { Application.showMessage("\n=========In script()========="); String jsonIn1 = context.getValue("Generated Data Source", "URLPARAM1"); def jsonFmt = new JsonSlurper().parseText(jsonIn1); Application.showMessage("jsonFmt: " + jsonFmt); }
URL format:
valx/cust/{parm1}/acdtls/{parm2}/txdtls/{parm3}/
URL request:
valx/cust/{"c-id":"1970840"}/acdtls/{"acno":"11TT9262","code":"GXIC200088","e-id":80,"zone":"P","exnt-no":"PP00881SS19703"}/txdtls/{"st-dt":"2020-10-30","end-dt":"2022-10-30","p-ind":M,"ttype":"X3","from-range":0,"to-range":119}
Data in the URL segment is not getting parsed into JSON format, getting below error
=========In script()========= groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object The current character read is '%' with an int value of 37 Unable to determine the current character, it is not a string, number, array, or object line number 1 index number 0 %7B%22acno%22:%2211TT9262%22,%22code%22:%22GXIC200088%22,%22e-id%22:80,%22zone%22:%22P%22,%22exnt-no%22:%22PP00881SS19703%22%7D ^
From URL, { character is encoded as %7B, " character is encoded as %22 which is causing the error.
Could you please suggest me a fix for this problem
Thanks,
Sati0 -
Hi,
I looked like the REST URL Data Bank might not be decoding the path segment. You can probably use the java.net.URLDecoder class to decode the value before parsing it as json.
0
Answers
-
Hi,
To answer 1 for what deployment path to use, either one will work. When using the longer path then you won't be able to have other responders in the same pva for the "valx" service. When using the shorter path you'll probably want to configure a responder correlation to make sure the request does have the expected path segments.
To answer 2 and 3 The REST URL Data Bank could help extract the entire path segment but then you'll need to use additional tools to get the values out of the json in the segment. You might be able to use a simple script to access the data banked variable and return its value to a chained JSON Data Bank which could extract the values from the specific tags.
0 -
Thank you so much William, could you please share more details on "You might be able to use a simple script to access the data banked variable and return its value to a chained JSON Data Bank which could extract the values from the specific tags."
Can you help me with sample script or knowledge article to get more details?
Thanks,
Sati0 -
Hi,
This documentation page is a good reference for scripting
https://docs.parasoft.com/display/SOAVIRT20241/Extensibility+or+Scripting+Basics
To get the JSON extracted from the path parameter into a JSON Data Bank I was thinking an Extension tool with the script below with a JSON Data Bank chained to the Extension tool's output.
value = context.getValue("Generated Data Source", "columnName")
return valueThis would take the value stored from the REST URL Data Bank and return it as input to the JSON Data Bank so that you could then extract whatever tags were needed.
0 -
Thanks William, I am able to use REST URL Data Bank and extract the path segment(s) into variables successfully. I will continue with below steps tomorrow.
- Setup JSON Data Bank to extract/convert JSON format path segment
- Setup Extension tool to use the JSON Data Bank variables and generate required response
Thanks,
Sati0 -
Thanks William,
URLDecoder class worked for me
Thanks,
Sati0