Last active
January 3, 2023 14:39
-
-
Save jgauthi/7c013a6342f1cdefc4b992d059a14291 to your computer and use it in GitHub Desktop.
Postman: Example request body created in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{body}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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