Service Virtualization

 View Only

 virtualize application/x-www-form-urlencoded REST service

Nagasree Devarakonda's profile image
Nagasree Devarakonda posted Dec 31, 2022 11:53 AM

Hi Team,

I am trying to virtualize "application/x-www-form-urlencoded" REST service using request and response pair and my request data is 

beneficiary.given:exact=Matt&beneficiary.family:exact=Clayhews&beneficiary.birthdate=1976-08-11&service-start-date=2020-01-01&service-end-date=2020-12-31

if i add only REST data protocol, the request data arguments are not visible. 
What are the protocol i need to add, other than REST data protocol ? 

Attached related screenshots. please suggest me on this.

Karl Miller's profile image
Broadcom Employee Karl Miller
Nagasree,
The easiest way to do this is change your RR Pair Request file. Add a "?" to the end of your path and then move your request data after the "?". 
This is how DevTest would see it if you did a recording because : "For application/x-www-form-urlencoded, the body of the HTTP message sent to the server is essentially one giant query string -- name/value pairs are separated by the ampersand (&), and names are separated from values by the equals symbol (=). "

Also don't forget to add "HTTP/1.1" at the end of the first line in a RR Pair Request.

New RR Pair Request 

POST /R4/Coverage/_search?beneficiary.given:exact=Matt&beneficiary.family:exact=Clayhews&beneficiary.birthdate=1976-08-11&service-start-date=2020-01-01&service-end-date=2020-12-31 HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Nagasree Devarakonda's profile image
Nagasree Devarakonda
Hi Karl,

With the help of your inputs, I could see the request data arguments after creating virtual service in workstation. Ideally, for POST call, we need to have below details

recorded_raw_request: beneficiary.given:exact=Jane&beneficiary.family:exact=DOE&beneficiary.birthdate=1966-08-10
HTTP-URI: /R4/Coverage/_search

Since we have added "?" to the end URI and then moved request data after the "?", i am seeing below changes for recorded_raw_request and HTTP-URI
recorded_raw_request: empty
HTTP-URI: /R4/Coverage/_search?beneficiary.given:exact=Jane&beneficiary.family:exact=DOE&beneficiary.birthdate=1966-08-10

while testing the virtual service in ITR mode, this scenario is working only if we pass request data in the URI. but i need to pass request data in request body since the method i am using is POST call.

Is there any way to send data in request body? if i pass request data in request body, virtual service is not working.
Attaching necessary screenshots, please provide your inputs



Attachments  View in library
image.png 123 KB
image.png 118 KB
image.png 280 KB
image.png 268 KB
image.png 152 KB
Karl Miller's profile image
Broadcom Employee Karl Miller
Nagasree,

Normally you could add the "Delimited Text Data Protocol" DPH (see DelimitedTextDataProtocol.JPG) and it would parse out the request, however 2 of your arguments beneficiary.given:exact and beneficiary.family:exact cause a SAX parser error due to having a ":" in the argument name. "org.xml.sax.SAXParseException; See https://stackoverflow.com/questions/59151503/org-xml-sax-saxparseexception-the-prefix-for-element-is-not-bound 

So I suggest adding a Scriptable Data Protocol DPH with the below Code to parse the body content to arguments. When you build from RR pairs I recommend adding a Request Data Manager DPH so you can see what you will get. When Trying this out I had the REST DPH then Request Data Manager DPH,  then Scriptable Data Protocol DPH, then Request Data Manager DPH. That way I could see the result of the Scriptable Data Protocol DPH and go make changes if needed. (see VS-Develop-DPH.JPG) 

code for Scriptable Data Protocol DPH:
%beanshell%

// You can use %beanshell%, %groovy% or %javascript% or some other installed JSR-223 scripting language
// This example is for beanshell

import com.itko.util.ParameterList;
import com.itko.util.Parameter;
import java.lang.Object;
import java.net.URLDecoder;

ParameterList args = lisa_vse_request.getArguments();

ParameterList metadata = lisa_vse_request.getMetaData();
// Looking up parameters
if (metadata.containsKey("Content-Type")) {
//int metSize = metadata.size();
String theContentType = metadata.getParameterValue("Content-Type");
if (theContentType.contains("application/x-www-form-urlencoded")) {
// Manipulate request body text
String theBody = lisa_vse_request.getBodyText();
String[] pairs = theBody.split("&");
for (String pair : pairs) {
int idx = pair.indexOf("=");
args.addParameter(new Parameter(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8")));
}
lisa_vse_request.setArguments(args);
// end if (theContentType=="application/x-www-form-urlencoded")
}
}
Nagasree Devarakonda's profile image
Nagasree Devarakonda
Thanks for the update. i will create virtual service as per your suggestion.