Skip to content

Instantly share code, notes, and snippets.

@jgauthi
Last active January 3, 2023 14:39
Show Gist options
  • Save jgauthi/7c013a6342f1cdefc4b992d059a14291 to your computer and use it in GitHub Desktop.
Save jgauthi/7c013a6342f1cdefc4b992d059a14291 to your computer and use it in GitHub Desktop.
Postman: Example request body created in Javascript
// 1. First route: Get data and index them in Postman Variable
// Set this content in Request > "Tests" Tab
// Step: You can get barcode with Sale > Get by ID
var jsonData = JSON.parse(responseBody);
if (jsonData.data.idSale) {
postman.setEnvironmentVariable("saleId", jsonData.data.idSale);
}
if (jsonData.data.saleItems) {
saleItems = [];
complements = [];
jsonData.data.saleItems.forEach(function(element) {
if (element.type !== 'ENT') { // element.type !== 'CMP'
return;
}
item = {
"id": element.id,
"idBookingItem": element.idBookingItem,
"pricingCode": element.pricingCode,
"idTicket": element.idTicket,
"idSeat" : (element.seat && element.seat.id) ? element.seat.id : null,
"idShowtime" : (element.showtimeInfo && element.showtimeInfo.idShowtime) ? element.showtimeInfo.idShowtime : null
};
saleItems.push(item);
});
postman.setEnvironmentVariable("bookingSaleItems", JSON.stringify(saleItems));
}
if (jsonData.data.complements) {
complements = [];
jsonData.data.complements.forEach(function(element) {
quantity = (element.quantity - element.quantityOnlineControlled - element.quantityCancelled);
if (quantity == 0) {
return true;
} else if (quantity < 0) {
window.alert('Quantity ' + quantity + ' incorrect for complement ID: ' + id);
}
item = {
"id": element.id,
"quantity": quantity
};
complements.push(item);
});
postman.setEnvironmentVariable("bookingComplements", JSON.stringify(complements));
}
// 2. Second route: Use postman Variable to body content
// Set this content in Request > "Pre-Request Script" Tab
// Step1: You can get barcode with Sale > Get by ID
// Step2: You must use the "Load Data" request before launch this script
bookingSaleItems = [];
if (pm.environment.get("bookingSaleItems")) {
bookingSaleItems = JSON.parse(pm.environment.get("bookingSaleItems"));
}
bookingComplements = [];
if (pm.environment.get("bookingComplements")) {
bookingComplements = JSON.parse(pm.environment.get("bookingComplements"));
}
requestBody = {
"idSale": pm.environment.get('saleId'),
"user": pm.globals.get('pm-username'),
"numCinema": pm.environment.get('cinemaId'),
"barcode" : pm.environment.get('barcode'),
"saleItems": bookingSaleItems,
"complements": bookingComplements
};
// https://community.getpostman.com/t/set-array-from-environment-variable-for-request-body-in-postman/4743/2
pm.environment.set("body", JSON.stringify(requestBody));
// Set this content in Request > "Tests" Tab
var jsonData = JSON.parse(responseBody);
if (pm.response.code == 200 && jsonData.data) {
pm.environment.unset("body");
pm.environment.unset("bookingComplements");
pm.environment.unset("bookingSaleItems");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment