Unable to pass the value from a variable/JSON file in the payload - karate

I am trying to pass a value inside below payload, I am able to get the value of it from the print but same is not accessible inside the payload, It prints the #(config.Secondary_ED) as the output.
The config.Secondary_ED contains an ip address. It works fine when I give ip address manually.
And print config.Secondary_ED
And request {"agent-forwarding":true,"configuration-data":"{\"service-host\":\"#(config.Secondary_ED)\",\"service-port\":\"8443\",\"user\":\"restdispatcher\",\"protocol\":\"vosrestdispatcher:rest\"}","configuration-template":null,"description":"Change Guardian Default Event Destination sk-12sp5","display-name":"sk-12sp5","ev-prototype-id":"1","forwarding-queries":[""],"id":1,"is-default":true,"is-indelible":true,"method":"vosrestdispatcher:rest","model":"REST Dispatcher","server-fordwarding":false}
When method PUT
Working Payload
#And print config.Secondary_ED
And request {"agent-forwarding":true,"configuration-data":"{\"service-host\":\"1.1.1.1\",\"service-port\":\"8443\",\"user\":\"restdispatcher\",\"protocol\":\"vosrestdispatcher:rest\"}","configuration-template":null,"description":"Change Guardian Default Event Destination sk-12sp5","display-name":"sk-12sp5","ev-prototype-id":"1","forwarding-queries":[""],"id":1,"is-default":true,"is-indelible":true,"method":"vosrestdispatcher:rest","model":"REST Dispatcher","server-fordwarding":false}
**Secondary_ED is stored in a json file called config.js
I have used # many times to get the value but same doesn't work in this case.
Please suggest what did I miss here.
Appreciate your help.

Please notice that the value of configuration-data is a string, not JSON object. Ask a friend who knows JS if this is not clear.
You can try this (note the usage of string):
* string configData = { 'service-host': '#(config.Secondary_ED)' }
* request { foo: '#configData' }
Or just use JS string concatenation (this is just a guess, please figure out what works for you, but you get the idea.
* def configData = '"{\\"service-host\\":\\"' + config.Secondary_ED + '\\"'

Related

Mule 4: uriParams size is showing as 0 even though it is there

So I created a endpoint inside the raml file such as:
/proxy:
/{proxyDestinationTarget}:
uriParameters:
proxyDestinationTarget:
type: string
example: "myurl.com"
post:
description: Pass through operation that targets IAA
is: [client-credentials-required,standard-error-responses,traceHeaders]
and then inside of the logic.xml in my variable component I have
attributes.uriParams.'proxyDestinationTarget'
when I send the request in postman and debug i am getting a uriParams size of 0
the url i entered in postman is
https://localhost:8092/proxy/uat.something.somethingElse.com/Assign/Assignment.svc
but if i send a request like this :
https://localhost:8092/proxy/uat.something.somethingElse.com
I get a uriParam size = 1 which is what I want. I guess the / is whats causing the problem. how can I pass url as uri param with escape characters???
It looks like you are not sending an URL that matches the RAML definition in the first case.
For the URL:
https://localhost:8092/proxy/uat.something.somethingElse.com/Assign/Assignment.svc
The RAML defined that the API should expect /proxy/{proxyDestinationTarget} but it is receiving something like /proxy/{proxyDestinationTarget}/Assign/Assignment.svc, where {proxyDestinationTarget} is uat.something.somethingElse.com, but nothing matches /Assign/Assignment.svc. The API should include those two last components too to match. It is not escaping them, they probably need to be defined.

Our base Url redirects to another URL which we can see in logs and this has a variable we need to store and pass in sub subsequent request

Using karate-UI
Given driver 'http://localhost:8080/auth/realms/auth?scope=openid&state=eferov08J37HlzbycjxHGs4.xzyoGFvM3QQ.test&response_type=code&client_id=hetg&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fauth%2Frealms%2Fendpoint'
* fullscreen()
And delay(2000)
And input('#username', 'username')
And input('#password', 'password')
When submit().click("#kc-login")
Then waitForUrl('http://localhost:8080/auth/realms/endpoint')
This URL contains a value which we want to extract. waitForUrl waits for this URL to come and once this URL is received how to proceed further. Is it possible to store this in some variable somehow? As all demos I saw is that input is mentioned or button can be clicked on this URL what about extracting a value from URL. How to store this URL so value can be extracted?
http://localhost:8080/auth/realms/endpoint?state=abbv&code=t6002231-3031-459f-b4c4-2e8a25223550.64f22bbc-6c28-49e4-bc2c-ca0ed40060de.36aee969-73e3-4bc5-bc5e-a4b68
Please read the documentation. waitForUrl() actually returns the value of the URL: https://github.com/karatelabs/karate/tree/master/karate-core#waitforurl
* def actualUrl = waitForUrl('/some/path')
Also see driver.url: https://github.com/karatelabs/karate/tree/master/karate-core#driverurl
* def actualUrl = driver.url

Karate / Post Request : Unable to pass random generator variable in json payload

I am trying to create Karate feature file for API Testing.
It is a post request and we have to pass random number for 1 field i.e. order in request payload every time.
Now, I am trying to use this function in the same feature file to pass that random generated value in json payload before sending post request.
Could someone please have a look at my feature file and help.
Thanks
Also, Is there a way if i want to pass this same random value for a json payload created as a separate request.json payload file
Your requestPayload is within double quotes, so it became a string.
Here's an example that should get you going. Just paste it into a new Scenario and run it and see what happens.
* def temp1 = 'bar'
* url 'https://httpbin.org/anything'
* def payload = { foo: '#(temp1)' }
* request payload
* method post
And please read the documentation and examples, it will save a you a lot of time.

Call to java script code returning the ASCII encoding for ':' separating key and value of returned object

I making an api request using karate where one of the api request params takes a filter condition (which is a java script object).
I am using a literal notation to create a java script object as shown below. This code is in a separate filter.js file.
function() {
var params = {
val1:[],
val2:[]
};
return params;
}
Now i call the above .js file in the karate scenario as below:
Scenario: Test
Given path 'filtertest/'
* param filter = call read('classpath:feature/common/filter/filter.js')
When method get
Ran the above and when i check the log, the api throws bad request error. I looked at the request url and there i can see that the ':' in the js file where I am assigning a value to a object key is replaced with %3A which i believe is the ASCII encoding for ':'. (the param with its values below)
?filter=%7B%22val1%22%3A%5B%5D%2C%22val2%22%3A%5B%5D
What I want is the ':' to come as it is from the .js call as the server side expects the filter param values as key value pairs.
Is there a way I can achieve this?
If your server cannot decode an encoded : it is a bug: https://www.w3schools.com/tags/ref_urlencode.asp
If you really need this - the workaround is to use the url keyword and build it manually, path and param will always encode.
Given url baseUrl + '/filtertest?filter=foo:bar'

How to log query string parameters with Postman

I'm using the tests of Postman to log into the console some of the details included in a JSON response.
The part of the test that logs is the following:
var data = JSON.parse(responseBody);
if (data.result[0] !== undefined) {
console.log(data.result[0].number, "|", data.result[0].category;
}
else console.log(QUERYSTRING_PARAMETER, "is not present");
I've tried many sintaxes/formats to have the value of the QUERYSTRING_PARAMETER passed to the test. However when data.result is empty with every sintax I've tried, the test simply logs QUERYSTRING_PARAMETER not defined. How can I pass the value from the query string parameters in the URL to the test to be evaluated/logged?
Thanks in advance
I'm not sure if it corresponds to what you need, but if you want data from your query, you may use the 'request' object (refer to the Postman sandbox, paragraph 'Request/response related properties').
You can get request method, url, headers and (maybe the one for you) data.
Alexandre