JMeter variables from one post http request to another - java

I've got a JMeter issue... The problem is: i'm sending a http request by POST method to a website and then the page returns me 100 responses, for example one of the answers:
Answer:
{
"Category": "XX",
"NumberX": 5100051,
"Class": "CLASS_1",
"NumberY": 5100136,
"arrivalDate": "2022-06-22T04:36:00",
"departureDate": "2022-06-21T23:58:00"
},
and then i have to save some of the values with a JSON extractor to variables.
Then I have second one http request by POST method, where in the body i have to use these variables. And my problem is that i don't how to count these variables in the body... With one data it's working, for more i don't know how to iterate it... Body in the second request:
{
"vehicleCategory": "${Category}",
"NumberOfTasks": "${NumberX}",
"departureDate": "${departureDate}",
"arrivalDate": "${arrivalDate_}"
}
I want to see query responses to each of the previous 100 values...
I hope i I clearly described the problem... Is there anybody who can help me to do it?

If you give "Match No" as -1 in the JSON Extractor:
it will generate the following JMeter Variables:
Category_1=first match
Category_2=second match
....
Category_matchNr=100 (or whatever is the number of matches)
So you will be able to iterate these values by:
Adding a Loop Controller with "Loop Count" set to ${Category_matchNr}
In the HTTP Request body reference the variables as:
{
"vehicleCategory": "${__V(Category_${__intSum(${__jm__Loop Controller__idx},1,)},)}",
"NumberOfTasks": "${__V(NumberX_${__intSum(${__jm__Loop Controller__idx},1,)},)}",
"departureDate": "${__V(departureDate_${__intSum(${__jm__Loop Controller__idx},1,)},)}",
"arrivalDate": "${__V(arrivalDate__${__intSum(${__jm__Loop Controller__idx},1,)},)}"
}
More information: Here’s What to Do to Combine Multiple JMeter Variables

Related

ContentType.URLENC with sending API REQUEST

I started API testing recently and facing different content-types each time, and i wan to know specific in what case we use ContentType.URLENC, I kinda sure we use it with x-www-form-urlencoded, but still confusing and we use it with usually POST request ?
Firstly, when you want to send infomation to server, you need to tell the server how you structure your message in BODY part. Below is some basic ContentType:
Using key-value with&: you can choose x-www-form-urlencoded.Since there are some special characters in your message, then these need to be encoded. That's why urlencoded.
Eg: name=abc&age=20
Json type: choose application/json .
Eg:
{
"name": "abc",
"age": 20
}
Want to send a file: choose form-data
Secondly, BODY part usually goes with POST and PUT request.

Unable to retrieve data from Sabre PriceQuoteServicesRQ 4.10 SOAP API: XML request schema validation failed: PriceQuoteInfo element is not complete

I have a problem when trying to retrieve data from Manage Price Quote Details (PriceQuoteServicesRQ) 4.10 Sabre SOAP API.
I generated Java classes using the WSDL from Sabre website (https://developer.sabre.com/docs/soap_apis/air/fulfill/manage_price_quote_details/resources).
I am constructing my request object in a following way:
ReservationTypeShort reservation = new ReservationTypeShort();
reservation.setValue("YEZUYS");
PriceQuoteInfoSearchParameters info = new PriceQuoteInfoSearchParameters();
info.setReservation(reservation);
PriceQuoteSearchParameters searchParameters = new PriceQuoteSearchParameters();
searchParameters.getPriceQuoteInfo().add(info);
searchParameters.setResultType(StringResultType.S);
GetPriceQuoteRQ req = new GetPriceQuoteRQ();
req.setSearchParameters(searchParameters);
req.setVersion("4.1.0");
I pretty-printed the object and this is what I got:
"priceQuoteInfo" : [ {
"reservation" : {
"value" : "YEZUYS",
"createDate" : null
},
"status" : [ ],
"type" : null,
"priceQuote" : [ ],
"travelItinerary" : null
} ],
So according to their documentation:
I am supplying all fields that are necessary, however it still doesn't work for me.
Did anybody else had the same problem? What am I missing/what am I doing wrong?
This is the error message I am getting:
XML request schema validation failed: PriceQuoteInfo element is not complete. One of the following fields: Status, Type, PriceQuote, TravelItinerary should be used. Please amend your request and try again.
What I have tried so far?
I asked Sabre Support for help, but they responded with a message that basically says "it works on my end".
I intercepted the XML body:
<ns5:GetPriceQuoteRQ version="4.1.0">
<ns5:SearchParameters resultType="S">
<ns5:PriceQuoteInfo>
<ns5:Reservation>YEZUYS</ns5:Reservation>
</ns5:PriceQuoteInfo>
</ns5:SearchParameters>
</ns5:GetPriceQuoteRQ>
I was missing an empty element <PriceQuote/> in my request.
It can be added by doing:
PriceQuoteInfoSearchParameters info = new PriceQuoteInfoSearchParameters();
info.setReservation(reservation);
info.getPriceQuote().add(new PriceQuoteSearch());
So according to their documentation I am supplying all fields that are necessary [...]
By documentation do you mean the WSDL or some human readable documentation (like PDF, DOCX, web pages, etc)? According to the error message you get, your SOAP request isn't valid. Sabre Support responding with "it works on my end" is another way of saying that you are not doing something correctly on your end. You need to troubleshoot your request.
From what I see, the error message is saying Status, Type, PriceQuote, and TravelItinerary but you are sending status, type, priceQuote, and travelItinerary. XML is case sensitive, and it's possible the service field names are too, so this might be the first thing to check.
Then, two of your fields (type and travelItinerary) are null. Also, priceQuote is empty. Is that OK? This is the next thing to check.
The object you pretty-printed shows a JSON format. Is this actually the format you are sending on the wire to the service? SOAP wants XML, not JSON. You also mention you generated the code from the WSDL. Using what framework or library? Does the generated code send XML?
Like I said, you need to troubleshoot the call:
download SoapUI
feed the WSDL file to SoapUI so that it can generate sample requests for you
fill in those request with real data and make calls to the web service until you get back a successful and expected response
using the same parameters from 3) in your code, perform the same call using your code
use SoapUI's monitoring tools to intercept the request at 4) and inspect the SOAP message you are sending
check the request you are making with your code against the successful request you got by using SoapUI directly
correct any differences until your request made by code is like the one send from SoapUI and it returns a successful and expected response.

To call two toD in apache camel spring dsl xml

I'm going to create one rest services in rest dsl xml. On that I have created one routes. For the route I am going to call my own microservices (this is created other project) for using toD uri. Once I get response I am going to take the values from the body (response json). After that again I am going to call other services in the same route based on the response values (we are taking one field in the response).
My question is
how we can take the values from the response in first service
And how to set headers in that respected values in first values..
How to call 2 services in route. Is it possible to call tod uri two times?
Sample code
<toD uri=http://localhost >
<log message =${body} >
(this response is going to set 2nd service query parameter value )
<toD uri=http://localhost? 1 services response values a>
Not sure if I fully understand your case, but here are my answers to your questions:
1) You can select any value from a JSON response with JsonPath. To use it later, you probably want to save it on the Message header
.setHeader("myHeader", jsonpath("$.your.json.path"))
2) Sorry, I don't understand this question :-)
3) Yes, you can make as many .to() or .toD() as you like
However, if you want to call REST services and you use the Camel REST component, you can profit from built-in URI templating. That means perhaps you don't need .toD()
For example
.to("rest:get:hello/{myHeader}")
would insert the value extracted from the JSON response above because the placeholder name is looked up in the message headers and if found replaced with the value of the corresponding message header
.setHeader("myHeader", jsonpath("$.your.json.path")) // assume jsonpath result is "world"
.to("rest:get:hello/{myHeader}") // URI "hello/world" is called

How to use "Priorities" in wiremock

I want to understand how priorities work. More specifically, what is the expected output of setting priorities to stub. There's limited documentation on this and the ones available doesn't really explain what the output would look like so I'm unable to verify if I have implemented it correctly.
This is my current code:
stubFor(post(urlMatching("/user/test\\?(and)\\=(que).*")).atPriority(1)
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/plain")
.withBody("This stub is testing for Title ")
)
);
System.out.println("About to execute the second stub");
stubFor(post(urlMatching("/user/test\\?(and)\\=(que).*")).atPriority(2)
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/plain")
.withBody("This stub is testing Author ID ")
)
);
System.out.println("Second stub executed");
I'm sending the following request from SOAPUI:
/user/test?and=query
Therefore both stubs should be executed and I should receive two responses correct?
I'm currently receiving only one response and that is from the stub that has priority 1. I'm not getting any response from the stub that has priority 2/
Can someone please help me on this?
What exactly do you want to achieve?
You normally have Wiremock configurations which have different request parameters or overlapping. In your case they are exactly the same. In which case should be the first and which case should the second be shown?
Wiremock will always return only exactly one answer. This answer is normally determined by evaluation of the request parameters you defined in your Wiremock configuration.
please refer to this description on how Wiremock and priorities work: Wiremock Stubbing and priorities
In case there are overlapping parameters, Wiremock will choose the configuration you added most recently (in your case the second).
Or you can guide Wiremock by setting priorities. A priority which is lower will be used in preference.
Normally you have a more general case (with less request parameters - as catch-up) and a more specific case. The first will get a higher prio (e.g. 9) and the latter a lower one (e.q. 5).
So the latter will be choosen in preference if request parameters match and in other cases the second one.
As #monsIgnore stated, for overlapping parameters the most recently added mapping that matches those parameters will be chosen.
When I initially looked at the request matching in wiremock I thought that the most exact match would be chosen.
By 'most exact' I mean the one that matched the most number of elements in the request. For example, given these two mappings (added in this order):
Mapping 1
"request" : {
"url" : "/oauth2/rest/consent",
"method" : "GET",
"headers" : {
"Cookie" : {
"equalTo" : "OAM_ID=VERSION_5"
}
}
}
Mapping 2
"request" : {
"url" : "/oauth2/rest/consent",
"method" : "GET"
}
If a request is received for url /oauth2/rest/consent and an OAM_ID cookie of VERSION_5 then the mapping that matches the most elements is Mapping 1.
However Mapping 2 is chosen since it matches and it was the most recently added.
In this case by adding priorities to the Mappings you can ensure that the request with the cookie is matched against Mapping 1.
Priorities are necessary because it's not always obvious what the most exact match is. For example if there was a third mapping:
Mapping 3
"request" : {
"url" : "/oauth2/rest/consent",
"method" : "GET",
"headers" : {
"Authorization" : {
"equalTo" : "Basic dXNlcjpwYXNzd29yZA=="
}
},
}
If a request arrived that had the cookie from Mapping 1 and the Authorization header from Mapping 3, then it's not possible to determine the most exact match for the request.
They both match 3 elements each. This is where priorities come in.
Setting stub priority is supported in WireMock.
By default, WireMock will use the most recently added matching stub to satisfy the request.
You can add priority in Java code by using atPriority:
stubFor(get(urlMatching("/api/.*")).atPriority(5)
or in JSON by adding:
"priority": 1
Read more on this here

Java Servlets: Differentiate between GET and POST

Is there a reliable way to separately extract GET and POST parameters using a HttpServletRequest?
That is, differentiate parameters that were sent in the query string (GET) to parameters that were sent in the request body (POST), assuming Content-Type: application/x-www-form-urlencoded.
Example
POST /path HTTP/1.1
Host: test.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 42
first_name=posted_foo&last_name=posted_bar
I would like to end up with two variables, one containing the values from the URL and one containing the values from the request body:
get = {"first_name": "foo", "last_name": "bar"}
post = {"first_name": "posted_foo", "last_name": "posted_bar"}
The only methods I seem to be able to extract these parameters are the getParameter* methods.
HttpServletRequest.getParameter: Returns a single string and tends to be the value provided in the URL (GET).
HttpServletRequest.getParameterValues: Returns an array of strings containing all of the values provided in the query string and request body. Those passed via the query string tend to appear first. However, if only one value is present in the returns array of strings, it cannot be reliably determined whether the value came from the query string or the request body.
To illustrate, using PHP these values are provided through the $_GET and $_POST superglobals.
The query string is trivial to parse, thus gives you the URI query param names, while the getParameterNames() gives you the whole set.
Split the query string by '&', then subsplit each token by '='.
For each key and value, perform the URLDecoder.decode(). That's all.
Toss all such keys in a set. If the param is in the uri query set it a good chance it's only there. If you must find if it is also in the post, actually, post form-encoded data is also coded like that, but that post is consumed so it's too late. Besides, the post could also be a multipart encoding which is non-trivial decode.
In the end, it's odd that you need this distinction. Can you explain for what purpose you seek this distinction?

Categories