Reputation: 771
How do i perfom multiple http request in redux saga for this payload:
[
{
"product_ids": [
"e331e78b-e954-4de3-9bb3-80827b0d0741"
],
"body": {
"command": "request",
"data": {
"api_key": "errtrt4i548",
"api_username": "xxxx",
"vendor_type": 1,
"from": {
"from_description": ""
},
"to": {
"to_name": "Fat Rain Films",
"to_lat": 36.77254990000006,
"to_long": -1.2711643,
"to_description": ""
}
}
}
},
{
"product_ids": [
"9331e78b-e954-4de3-9bb3-80827b0d0741"
],
"body": {
"command": "request",
"data": {
"api_key": "errtrt4i548",
"api_username": "xxxx",
"vendor_type": 1,
"from": {
"from_description": ""
},
"to": {
"to_name": "Fat Rain Films",
"to_lat": 36.77254990000006,
"to_long": -1.2711643,
"to_description": ""
}
}
}
}
]
And my Saga code is :
export function* getQoute({ qoute }) {
yield put(updateNetworkStatus(true));
const headers = {
method: 'POST',
// credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(qoute),
};
const total = 0;
try {
const req = yield call(
request,
`${API_BASE}/shippings/shipping/initiate_delivery_sendy/`,
headers,
);
console.log(req);
// total += req.
} catch (err) {
console.log(err.message);
//yield put(updateLoadingStatus(false));
}
}
The challenge i am experiencing is finding a way to call my api with the data in my payload. i.e Call the api 2 times or as much as the number of objects in my json payload in the same saga or action.
Upvotes: 2
Views: 679
Reputation: 89
For your case you can iterate through the json array and dispatch an action to call your saga with each of the object as payload.
Say I have an array payloads[2] = {obj1, obj2}. Then do
var i;
for (i = 0; i < payloads.length; i++) {
yield put({type: YOUR_ACTION_TYPE_FOR_SAGA, payload: payloads[i]});
}
Upvotes: 2