JMETER - Fetch data on response data and be a variable on API - java

Thanks for helping!
I'm trying to get a ID from a Response Data, but i tryied everything and didn't work, let me show:
I want that ORGKEY as a Variable
I run POST, after they give me a ORGKEY
And i need the ORGKEY in POST TOKEN
POST TOKEN needs a orgKey
And the orgkey are a random numbers and letters! I tryed everything but didn't work, so i try here.
Thank you for your pacient ;)

Add JSON Extractor as a child of the request which returns this JSON response
Configure it as follows:
Names of created variables: anything meaningful, i.e. ORGKEY
JSON Path Expressions: $..orgKey
Match No.: 1
That's it, you should be able to refer the extracted value as ${ORGKEY} where required
More information:
JsonPath Examples
API Testing With JMeter and the JSON Extractor

Your JSON response
{
"result":{
"returnCodel":"success",
"orgKey":"3b6car5lkckvgx52wf",
"message":"Organizaçào criada com sucesso!"
}
}
You can use JSON Extractor or JSON JMSEPath Extractor to extract the orgKey from the response.

Related

How to combine JSON values in the response using Java

I am currently working on a school project. We have a series of response templates in JSON format that will take values from the request and then return it accordingly in the response when run in postman.
e.g
Request:
{
"Application_id":123456
}
Response:
{ "Application_id: 123456, TIMESTAMP: 20220501}
I am able to get these values in the response but the issue I am running accross now is figuring out how to combine 2 values in the request into one like so:
Request:
{
"Application_id":123456
"user_id_first_six": 456789
"user_id_last_four": 1234
}
Expected Response:
{ "Application_id: 123456, TIMESTAMP: 20220501, combined_id:456789****1234}
what I have tried is to put combined_id : "user_id_first_six"+******+"user_id_last_four" but this doesnt work.
Apologies if I cant be more specific as there are portions that I have left out due to confidentiality issues.
The easiest way to achieve this in Java would be to use JSONObject. In your Request-Handler, add two parameters of Type JSONObject and then merge them:
jsonObj.putAll(jsonObj1)
Thanks all for the guidance. I basically did what Knu8 suggested and extracted the values using Matcher+Regex (<(.*)>)(\W*)(<(.*)>) and converted them to strings and then used StringBuilder to append all the components together.

How to store Soap Response Message in a string using citrus framework

I have a situation where I need to store my SOAP response in a string in case of success.
soap(soapActionBuilder -> soapActionBuilder.client("xyzclient").receive().messageType(MessageType.XML).validate("xapth validation", "Success"));
The above code is working if we receive a success response, but now I need to store that SOAP response in a string and return it.
I am not sure how I can do that. if anyone is having any idea please share, I am new to citrus. Thanks in advance.
If you would like to capture some value from response body, you could use a method extractFromPayload. This methods takes two parameters:
path - path to XML element
variable - to store element into variable for further usage in citrus test.
Sample how to use it:
soap().client("client")
.receive()
.extractFromPayload("//Foo/bar","foobar");
Now you can use variable foobar like this ${foobar}
echo("Extracted var: ${foobar}")

REST services PATCH API example

I am trying to call the REST Webservices PATCH API, here is My JSON payload
[
{ "op":"replace", "path":"/values/Timestamp","value":"2016-10-28T15:25:43.511Z"},
{ "op":"replace", "path":"/values/Flag", "value":true },
{ "op":"replace", "path":"/values/Flow", "value":"Flow A"},
{"op":"replace", "path":"/values/Interests", "value":[ "Sports", "Book Reading" ] }
]
JSON Value attribute has different values with different data types. and I want to prepare Entity object(Java) and convert it into JSON and call REST end point.
now
I am not very sure
which is the best suitable data type I can choose for values attribute
I have referred following links but I didn't get enough details
Android REST API using PATCH method
https://www.rfc-editor.org/rfc/rfc5789#section-2.1
http://blog.earaya.com/blog/2013/05/30/the-right-way-to-do-rest-updates/
http://williamdurand.fr/2014/02/14/please-do-not-patch-like-an-idiot/
but I didn't get enough details.
any suggestion on this is really appriciated
Got the java object from the client and created another Java class with below properties and set the values
opn - string
path - String
value - Object
added above java objects to array list then used the GSON library to convert it into the array of JSON objects which will be accepted by patch api.
and please note the content type is application/json-patch+json

Search Shopify products by keyword with JSON response

I am building an application for a shopify store and have a requirement of displaying products using a keyword that the user enters.
I searched over and found this url working:
...e.myshopify.com/search?q=short&type=product&view=json
where I append the keyword to the ?q variable.
But i need a JSON response for my application. It does not gives a Json response even after mentioning the view=json field.
Pleas help.
Thanks in advance.
The trick is to write a simple template in your theme to render the JSON you need in response to that JSON view. Just create the file templates/search.json.liquid, which can be as simple as the following:
{% layout none %}
{{ search.results | json }}

Passing Jackjson JSON object from JSP to JavaScript function

I have a JSON String stored in a database. In one of my JSP pages, I retrieve this string, and I want to be able to pass the String or the JSON object into Javascript function. The function is simply this for test purposes
function test(h){
alert(h);
}
Now I can retrieve the JSON string from the database fine, I have printed it out to the screen to ensure that it is getting it, however when I pass it in like this
<input type="button"
name="setFontButton"
value="Set"
class="form_btn_primary"
onclick="test('<%=theJSON%>'); return false;"/>
Nothing happens. I used firebug to check what was wrong, and it says there is invalid character.
So I then tried passing in the JSON object like so
Widget widg = mapper.readValue(testing.get(0), Widget.class);
Then pass in it
onclick="test('<%=widg%>'); return false;"/>
Now this will pass in without an error, and it alerts the object name, however I am unable to parse it. Object comes in like with the package name of where the widget class is stored like so
com.package.mode.Widget#ba8af9
I tried using Stringify, but that doesn't seem to work on this Jackson JSON object.
After all that failed, I tried a last resort of taking the String from the database, and encoding it in base64. However, this too fails if I do this
String test = Base64.encode(theString);
and pass that in. However if I do that, print it out to the screen, then copy what is printed out, and send that through it works, so don't quite understand why that is.
So could someone please tell me what I am doing wrong. I have tried soo many different solutions and nothing is working.
The JSON String is stored in database like this
{
"id":1,
"splits":[
{
"texts":[
{
"value":"Test",
"locationX":3,
"locationY":-153,
"font":{
"type":"Normal",
"size":"Medium",
"bold":false,
"colour":"5a5a5a",
"italics":false
}
}
]
}
]
}
Would be very grateful if someone could point me in the direct direction!!
Edit:
Incase anyone else has same problem do this to pass the JSON from JSP to the JS function
<%=theJSON.replaceAll("\"", "\\\'")%>
That allows you to pass the JSON in,
then to get it back in JavaScript to normal JSON format
theJSON = theJSON.replace(/'/g,'"');
Should work fine
I think the combination of double quotes wrapping the onclick and the ones in your JSON may be messing you up. Think of it as if you entered the JSON manually -- it would look like this:
onclick="test('{ "id":1, "splits":[ { "texts":[ { "value":"Test", "locationX":3, "locationY":-153, "font":{ "type":"Normal", "size":"Medium", "bold":false, "colour":"5a5a5a", "italics":false } } ] } ] }'); return false;"
and the opening double quote before id would actually be closing the double quote following onclick= (You should be able to verify this by looking at the page source). Try specifying the onclick as:
onclick='test(\'<%=theJSON%>\'); return false;'
You can follow the following steps
Fetch the jon string
Using the jackson or any other JSON jar file , convert the json string to json array and print the string using out.println.
Call this jsp which prints the json string
check in the firebug , you will be able to see your json .
If the Json string does not print , there can be some problems in your json format.
this is a good website for json beautification , http://jsbeautifier.org/ , really makes the string simple to read .
Thanks
Abhi

Categories

Resources