Hi everyone!
I would like to know if there is a solution to my problem, let me explain it:
I am using the pm.sendRequest and I would like to send params that are stored in an array called ids. But I did not find throughout the web how to accomplish that. I tried another way and it is working but not as desired, have a look:
This code works but send multiple DELETE requests
First it retrieves all course ID and stores them in an array. After that, for each course ID, it sends multiple DELETE requests.
const URL = pm.environment.get('base_url'), TOKEN = pm.environment.get('admin_token');
// Get All Courses
pm.sendRequest({
url: `${URL}/content-manager/explorer/application::course.course`,
method: 'GET',
header: {
'Authorization': `Bearer ${TOKEN}`
}
}, function(err, res){
let response = res.json(), ids = _.map(response, ({ id }) => ( id ));
// Delete Courses
if (typeof ids !== 'undefined' && ids.length > 0) {
_.forEach(ids, (id, index) => {
pm.sendRequest({
url: `${URL}/content-manager/explorer/deleteAll/application::course.course?${index++}=${id}`,
method: 'DELETE',
header: {
'Authorization': `Bearer ${TOKEN}`
}
}, function(err, res){
pm.test('Code is 200'), function() {
pm.expect(responseBody.code).to.be.equal(200);
}
});
});
}
});
As you could see, I am looping through each ID and sending a DELETE request. But I can send multiple params in just one request, like ...course.course?0=id1&1=id2&...
. For this, I can use the following code in one request:
This code is placed in the Postman Pre-request Script
Backing up the code in case image fails
const URL = pm.environment.get('base_url'), TOKEN = pm.environment.get('admin_token');
// Get All Courses
result = pm.sendRequest({
url: `${URL}/content-manager/explorer/application::course.course`,
method: 'GET',
header: {
'Authorization': `Bearer ${TOKEN}`
}
}, function(err, res){
let response = res.json(), ids = _.map(response, ({ id }) => ( id ));
_.forEach(ids, (id, index) => {
pm.request.url.addQueryParams(`${index++}=${id}`);
});
});
Question
How can I move the following block to the first example to make just one DELETE request? I mean, I want to use an alternative to pm.request.url.addQueryParams()
to the pm.sendRequest()
within it.
_.forEach(ids, (id, index) => {
pm.request.url.addQueryParams(`${index++}=${id}`);
});
By the way, the array structure is simple: [1, 2, 3, …], the index is autogenerated by the loop.