Can i create a feeder from extracting response from 1 request and pass the feeder values to next request in Gatling - gatling

When i run my simulation file : it runs fine and response body looks like this
[
{
"id":1,
"region":US
},
{
"id":3,
"region:EUR
}
]
i am able to get a vector with values from id [1,3,....]
how can i convert this to iterator.continually(Mapvalues) of above feeder.

Step 1: In this request, you can use check to save all ids to the session
.check(jsonPath("$[*].id").findAll.saveAs("ids")));
Step 2: In the next request, get this data from the session to do
.feed(data)
.exec(request1)
.foreach("${ids}", "id") {
exec(request2)
}

Related

OCAPI Batch request to get all Orders after a Certain Date

I'm New to SFCC OCAPI. My purpose is to Export all Orders from "development.demandware.net" after certain date and this can happen Quite frequently like once in every 2 days. I'm currently using Python to achieve this using the endpoint "s/{{SITEID}}/dw/shop/v18_1/order_search". The problem is One call is getting me only 25 Records. Again i have change the query from dynamically to start from RecordNo 26 for the next call. So, If I have like 10,000 records, it makes upto 400 calls everytime the scirpt runs. The alternative options i'm aware of is:
OCAPI Batch requests
OCAPI Export job (Tried this, but haven't got enough knowledge to set this up)
So, I'd like to know if my purposed is achievable using the batch request. I tried to do this using the documentation. And, the response was 200 with no response body using the below code.
url = f"https://{DOMAIN}/s/-/dw/batch"
url_param = {'client_id': CLIENT_ID}
header = {'Authorization': 'Bearer ' + token,
'Origin': f'https://{DOMAIN}',
'Content-Type': 'multipart/mixed;boundary=23dh3f9f4',
'x-dw-http-method': 'POST',
'x-dw-resource-path': 's/{{SITEID}}/dw/shop/v18_8/order_search'}
body = """
{
"query" :
{
"filtered_query": {
"query": { "match_all_query": {} },
"filter": {
"range_filter": {
"field": "creation_date",
"from": "%s",
"from_inclusive": true
}
}
}
},
"select" : "(**)",
"sorts": [{
"field": "order_no",
"sort_order": "asc"
}],
"start": %s
}""" %(RETRIVE_RECORDS_FROM, startRecordFrom)
response = requests.post(url, params = url_param, headers = header, data = body)
My code doesn't have a x-dw-content-id as the above is an initial request. If its possible to achieve my purpose,
how should my sub-request looks like.?
And after that how do i retrieve the data of my request.? is there any endpoint i should use to get the batch results.?
I maybe asking for too much information. But, i couldn't find much information about this online so had to ask every question i have in one post.
My question might look similar to this question "Salesforce Commerce Cloud/Demandware - OCAPI query orders by date range", But I'm looking for information about batch requests and also to reduce the number of API calls.
Thanks in advance.

response.map is not a function

I'm trying to loop a request to extract the ID's from a previous request. I followed the steps in this video https://www.youtube.com/watch?v=4wuvgX-egdc but i can't get it to work. As I see it the problem is that {} is not an array but I would like to search within "campaigns" which seems to be an array. (As you probably understand I'm new to this)
Here's the request I've sent and would like to loop through to extract the ID's that I wish to use in the next request. (there are several hundreds of ID's)
{
"campaigns": [
{
"id": 373894,
"name": "Benriach",
"created_at": "2022-01-21 13:37:34",
"sent_at": "2022-01-21 13:37:53",
"status": "sent",
"type": "text_message"
},
Here's the test that I'm trying to run.
const response = pm.response.json();
const campaignids = response.map (campaignid => campaigns.id);
console.log(campaignids);
pm.variables.set('campaignids', campaignids);
Here's how it looks>>
Screenshot
The end goal is to use Postman to extract campaign statistics from an e-mail marketing tool and then send it on into Google Data Studio where I want to create a dashboard for e-mail-campaigns using both data from the e-mail marketing tool as well as website data.
const campaignids = response.map (campaignid => campaigns.id); here is the problem
space between map (
const response = pm.response.json();
const campaignids = response.map(campaign => campaign.id);
console.log(campaignids);
pm.variables.set('campaignids', campaignids);
and make sure response should be an array

How can I combine JSON rows in a logic app grouped by another property

I have a logic app that is taking failed runs from an app writing to application insights, and I want to group all the errors by the operation name into a single message. Can someone explain how to do this?
my starting data looks like:
[{ "messageError": "Notification sent to AppName but not received for request: 20200213215520_hUu22w9RZlyc, user email#email.com Status: NotFound",
"transactionKey": "20200213215520_hUu22w9RZlyc"},
{ "messageError": "App to App Import Request: 20200213215520_hUu22w9RZlyc from user email#email.com was unable to insert to following line(s) into App with error(s) :\r\n Line 123: Unable to unlock this record.",
"transactionKey": "20200213215520_hUu22w9RZlyc"}]
What I am trying to get out of that would be a single row that concatenates both messageError values into one statement on a common transaction key. Something like this:
[{ "messageErrors": [{"Notification sent to AppName but not received for request: 20200213215520_hUu22w9RZlyc, user email#email.com Status: NotFound"},
{"App to App Import Request: 20200213215520_hUu22w9RZlyc from user email#email.com was unable to insert to following line(s) into App with error(s) :\r\n Line 123: Unable to unlock this record."}],
"transactionKey": "20200213215520_hUu22w9RZlyc"}]
There might be as many as 20 rows in the dataset, and the concatenation needs to be smart enough to group only if there are multiple rows with the same transactionKey. Has anyone done this, and have a suggestion on how to group them?
For this requirement, I thought that we can use liquid template to do the "group by" operation for your json data at the beginning. But according to some test, it seems azure logic app doesn't support "group by" in its liquid template. So there two solutions for us to choose:
A. One solution is do these operations in logic app by "For each" loop, "If" condition, compose the json data and so many other actions, and also we have to initialize many variables. I tried this solution first, but I gave up it after creating so many actions in logic app. It's too complicated.
B. The other solution is call a azure function in logic app, and we can do the operations for the json data in function code. It's not easy either, but I think it's better than the first solution. So I tried this solution and got success. Please refer to the steps below:
1. We need to create a azure function app with a "HTTP" trigger in it.
2. In your "HTTP" trigger, please refer to my code below:
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
string body = await req.Content.ReadAsStringAsync();
JArray array = JArray.Parse(body);
JArray resultArray = new JArray();
JObject tempObj = new JObject();
foreach (var obj in array)
{
JObject jsonObj = JObject.Parse(obj.ToString());
string transactionKey = jsonObj.GetValue("transactionKey").ToString();
string messageError = jsonObj.GetValue("messageError").ToString();
Boolean hasKey = false;
foreach (var item in tempObj)
{
JObject jsonItem = (JObject)item.Value;
string keyInItem = jsonItem.GetValue("transactionKey").ToString();
if (transactionKey.Equals(keyInItem))
{
hasKey = true;
break;
}else
{
hasKey = false;
}
}
if (hasKey.Equals(false))
{
JObject newObj = new JObject();
JArray newArr = new JArray();
newArr.Add(messageError);
newObj.Add("transactionKey", transactionKey);
newObj.Add("messageErrors", newArr);
tempObj.Add(transactionKey, newObj);
}
else
{
JObject oldObj = (JObject)tempObj.GetValue(transactionKey);
JArray oldArr = (JArray)oldObj.GetValue("messageErrors");
oldArr.Add(messageError);
oldObj.Property("messageErrors").Remove();
oldObj.Add("messageErrors", oldArr);
tempObj.Property(transactionKey).Remove();
tempObj.Add(transactionKey, oldObj);
}
}
foreach (var x in tempObj)
{
resultArray.Add(x.Value);
}
return resultArray;
}
3. Test and save the function, and then go to your logic app. In logic app, I initialize a variable named "data" with the json data below to simulate your scene.
4. Then create function in your logic app and choose the "HTTP" trigger which you created just now.
5. After running the logic app, we can get the result shown as below:
[
{
"transactionKey": "20200213215520_hUu22w9RZlyc",
"messageErrors": [
"xxxxxxxxx",
"yyyyyyyy"
]
},
{
"transactionKey": "keykey",
"messageErrors": [
"testtest11",
"testtest22",
"testtest33"
]
}
]

How to send array index value into JSON body in Postman

I'm chaining several requests to simulate an end to end scenario in postman.
I have an array of values which was returned back from the first request.
I've set the array as an environment variable so that it can be accessed by other subsequent requests in my collection.
pm.environment.set("travArray", travArray);
The console output of the travArray looks like:
0:"d4faf286-ab68-448b-87cb-ad6a7030bc57"
1:"2e21ab1f-25be-49ab-a984-db529022cf0f"
2:"9a1d942c-4048-48a7-acfc-7a1c9563c528"
Next, I'm trying to post a json request with each of these indexed values.
In the Body (raw) request it looks like this;
"travelers": [
{
"id": "{{travArray[0]}}",
"firstName": "teaaf",
"lastName": "fadfadsfads",
"travelerType": "ADULT"
},
{
"id": "{{travArray[1]}}",
"firstName": "sdfsfsdf",
"lastName": "sdfsfsd",
"travelerType": "ADULT"
},
{
"id": "{{travArray[2]}}",
"firstName": "sdfsdf",
"lastName": "sdfsfsdf",
"travelerType": "ADULT"
}
],
But using this syntax {{travArray[1]}} fails to parse the actual value when the raw request is posted to the API endpoint.
Interestingly, when i just use {{travArray} - without the index notation, the entire contents of the array is sent.
example output:
id:"d4faf286-ab68-448b-87cb-ad6a7030bc57,2e21ab1f-25be-49ab-a984-db529022cf0f,9a1d942c-4048-48a7-acfc-7a1c9563c528"
firstName:"teaaf"
lastName:"fadfadsfads"
travelerType:"ADULT"
Can someone tell me how to parse index value of the array in the json body request?
Thanks!
I'm new to Postman scripting, so this is my noob method of doing this.
You need to pass the array location into the test script.
var jsonData = JSON.parse(responseBody);
pm.environment.set("travArray", travArray[0].id);
You can iterate through the array by setting a variable and then using the .setNextRequest() method.
Set an index variable in your environment. Then set a for-in loop:
var jsonData = JSON.parse(responseBody); // Deserialize the return
for (let traveler in jsonData) {
pm.setNextRequest(jsonData[traveler].id);
// do any checks/tests with the response.
}
Once you've processed the other request, you can add a .setNextRequest() to the script and point it back to the first request. With the .setNextRequest(null) call, Postman will stop running the iteration.
Good luck!

Restangular set the correct ID

I have the problem that the API Im working against always returns the data wrapped inside an array.
So if I call GET api/companies/1/jobs/1 the response looks like this
{
data: [
{
id: 1
name: "blabla"
}
]
}
When I want to update this job and sent a put the URL is being build wrongly.
Instead of PUT api/companies/1/jobs/1 the request is sent to api/companies/1/jobs and gets rejected.
I have tried the following
RestangularProvider.setRestangularFields({
id: "data[0].id"
});
but unfortunately this does not resolve my problem.
Is there any way to make this work with API being setup as it is?

Resources