What are response objects sent through Kaizala API? string or JSON - kaizala

Following is the response being sent
userIdToResponsesMap:
1. 10ce550c-4ee2-44a5-99d3-92552c9e8cfa:
0:"["65431.35","12606.12","11637.09","4848.06"]"
1:"["dealer-2","dealer-3","dealer-3","d-1001"]"
Above values look like an array, but in JS, usually arrays do not have the beginning and ending " (quotes). So, is there something else that I am missing here

This is a json string. To retrieve the array you can do var array = JSON.parse().
In your example, following should work
var array = __responses.userIdToResponsesMap["10cde550c-4ee2-44a5-99d3-92552c9e8cfa"][0]

Related

How to pass parameters to feature file in a loop? [duplicate]

I am making a SOAP request, and I am receiving the response that's returned as an array:
- [print] [
"M4205N",
"M4206U"
]
For each item in the array, I want to make another SOAP request. I've read how you can do this with tables and call a feature file, and I've read how to loop through an array, and call a js function. I cannot figure out how to loop through the array, and pass each value to another SOAP request XML (one at a time).
I want to do something like this:
Given soapURL
And method post
def responseArray = /xml path for the codes I want/
def result = call read('otherRequest.feature') responseArray
The otherRequest.feature file would look something like this:
#ignore
Feature:
Background:
* def myNewRequest = read('soap.xml')
Scenario:
Given soapURL
* replace myNewRequest
| token | value |
| ##refNum## | responseArrayValue |
When request myNewRequest
And method post
However, I get this error:
GetNewMessageList.feature:27 - argument not json or map for feature call loop array position: 0, M4205N
How can I loop through each item in the array, and pass each value to the other feature file?
The addition of this one line should do what you want. Yes there is a hard requirement that the "loop" array should be an array of JSON objects. But you can convert an array of primitives in one step:
* def responseArray = karate.mapWithKey(responseArray, 'responseArrayValue')
This is documented here: https://github.com/intuit/karate#json-transforms

Karate match two json files(expected json and API response) irrespective of the order of array elements

Expected Response:
{"data":{
{"assignments":[{"locationId":"1186755","locationName":"X.11.11"},{"locationId":"1186756","locationName":"X.11.12"}]}}}
Response:
{"data":{
{"assignments":[{"locationId":"1186756","locationName":"X.11.12"},{"locationId":"1186755","locationName":"X.11.11"}]}}}
I saw a SO post stating to use karate.sort(response, x=>x.locationId), when i tried it's giving me empty response. Is there any simple way i can achieve do the comparison of whole response file irrespective of order?
Note: I even tried contains only, but it's failing the assertion.
Just use contains deep: https://stackoverflow.com/a/64373344/143475
* def response = {"assignments":[{"locationId":"1186755","locationName":"X.11.11"},{"locationId":"1186756","locationName":"X.11.12"}]}}}
* match response contains deep {"assignments":[{"locationId":"1186756","locationName":"X.11.12"},{"locationId":"1186755","locationName":"X.11.11"}]}}}

unable to convert raw data to json format after replacing the \(slash)

I am passing the raw data and removing unwanted slash and trying to convert to json format, unable to proceed.
json spot = {"LoginDetails":"{\"firstName\":\"abcd\",\"lastName\":\"\",\"middleName\":\"\",\"phoneNumber\":\"6944000000\",\"phoneCountryCode\":\"+91\",\"dob\":\"1945-02-22\"}"}
string a = spot
def ab = a.replace("\\", "")
json st = ab
And request st
When method POST
Then status 200
Getting error
net.minidev.json.parser.ParseException: Unexpected token f
How to resolve this issue and convert the above payload into json format?
NOTE: After conversion to json firstname, phonenumber should be passed dynamically from the preceding API calls.
If u want escape slash with json, you could try
a.replaceAll("\\", "\\\\")
or change it to unicode.
a.replaceAll("\\", "\u005C")
Both of them is workable for browser but not sure for karate, maybe you need change a bit by yourself.

how to pass multiple type of data to API from server side as formdata?

I want to create circuit (messenger like skype) conversation from my server side code (C#) by calling circuit api.
Function : https://circuitsandbox.net/rest/v2/conversations/
I have to pass two type of parameters :
1) Participants - string array
2) topic - string
According to function definition (in swagger), I have to pass them as formdata. But, when I am trying to encode the parameters
var content = new FormUrlEncodedContent(values);
It is not accepting the array string for participants list. It is expecting "key/value" pair as "string/string".
I have even tried to create the JSON serialization also
JsonConvert.SerializeObject(values)
But the API definition is not accepting this converted values as it is expecting formdata in string/string as key/value.
I had even tried to concatenate the participants list with ";" as delimiter. But in that case, I am getting 400 error.
I tried to convert my parameters like this also
var formData = new List>();
formData.Add(new KeyValuePair("participants", JsonConvert.SerializeObject( participants)));
formData.Add(new KeyValuePair("topic", "Testing1"));
But again, I a getting 400 error.
Here is my API call
var response = client.PostAsync("https://circuitsandbox.net/rest/v2/conversations/group", content);
Can someone provide me some code snippet to pass that this data to API?
Let me emphasis, I am trying from server side code (C#) and not jquery code.
as we checked together earlier today, make the participants value a comma separated string of emails and it should work

using req.params to work with an array is failing

var myArray = [1,2,3,4];
If I post to 'api/myArray' and I want to retrieve these values, the following does not work:
'api/:var'
field : {blah: req.params.var[0], blah2: req.params.var[1]}
This does not work, and after console I realized it is no longer an array. Instead, var is now 1,2,3,4 instead of [1,2,3,4]. How do I solve this?
My excuses. I should really have tried harder to find out how to do this.
req.params.var.split(',') would do this
1st:
while sending you can serialize it
JSON.stringify(var)
and recieve it as
JSON.parse(var)
2nd:
Post with formdata instead
you can use req.body instead of params
then you can access like
req.body.var[0]