
Use Postman Runner to turn CSV data into bulk API requests
Previous to my role working as a Customer Success Engineer with a SaaS platform, I'd only ever used Postman to do simple API testing such as when building out little MERN stack apps.
Turns out I was just scratching the surface of Postman's capabilities.
Let's talk about Postman Runner!
If you've ever been in a situation where you need import data in bulk from a CSV via the API, but just end up arbitrarily sending single POST requests or writing lengthy scripts to insert data directly into the DB, then keep reading!
For this demo, I'm going to use the Reqres users endpoint to POST some data to.
My data is going to be coming from a CSV that looks like this:First things first (sips coffee) let's get our Postman set up. To use a runner, we'll need a collection that contains our API request.
To do this, go ahead and create a new collection in your workspace:Let's also go ahead and add a test script to the Post-response:
pm.test("Status code is 201", function () {
pm.response.to.have.status(201);
});
This script will run after each request in the collection. It checks if the response status code is 201. If the status code is anything other than 201, the test will fail, and you'll be able to identify which requests didn't pass the test.
In the collection, add a new POST request to the /api/users endpoint:Next, we'll need to setup the request Body. Instead of passing a single string value for each
name
and job
, we'll be using variables to reference the row header of the CSV:
{
"name": "{{name}}", //A1 header row
"job": "{{job}}" //B1 header row
}
We're ready to run the collection! Click the three dots next to the collection > Run collection:
Select File, and browse to the the CSV:
You'll see here how many times the request will loop through the CSV - this should match the number of rows in your CSV (excluding the header).
Go the Advanced settings > and check that Persist responses for a session is checked. This option is useful for keeping track of the responses you receive during a collection run.
Ready to Run!