Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ivangeorgiev/d47ea65fe472b557454b57d92694f7e3 to your computer and use it in GitHub Desktop.
Save ivangeorgiev/d47ea65fe472b557454b57d92694f7e3 to your computer and use it in GitHub Desktop.
Loop Over Data File Postman Collection
{
"info": {
"_postman_id": "ef0a1444-9fe4-4798-91ce-5febefd02bba",
"name": "Loop Over Data File",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "7243476"
},
"item": [
{
"name": "Do Post",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
"let currentUsername = pm.iterationData.get(\"username\");\r",
"\r",
"if (!currentUsername) {\r",
" currentUsername = pm.collectionVariables.get(\"username\");\r",
"}\r",
"\r",
"if (!currentUsername) {\r",
" throw new Error('Collection variable \"username\" is not set. There are two possible solutions: (1) define current value for the collection variable \"username\" and send the request - upon completion the variable will be unset automatically, or (2) run the collection loading CSV file with \"username\" column.')\r",
"}\r",
"\r",
"pm.collectionVariables.set(\"username\", currentUsername);\r",
""
],
"type": "text/javascript"
}
},
{
"listen": "test",
"script": {
"exec": [
"const username = pm.collectionVariables.get(\"username\");\r",
"\r",
"pm.test(`Status code is 200 for username: '${username}'`, function () {\r",
" pm.response.to.have.status(200);\r",
"});\r",
"\r",
"pm.collectionVariables.unset(\"username\")\r",
""
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"username\": \"{{username}}\"\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://postman-echo.com/post",
"protocol": "http",
"host": [
"postman-echo",
"com"
],
"path": [
"post"
]
}
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
}
]
}
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
username
Ivan
BigBaobab
Roshko
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment