Instructor: In this video, I'll be answering a question
that came up in the Postman Community Forum.
Grant wants to know how to handle a CSV response
with Postman, so let's have a look.
(upbeat music)
As I was telling in the introduction,
I wanted to show you in this video
how to handle CSV responses with Postman.
Now, we have here a very clear example that came from Grant,
and he's getting this comma-separated values list,
and he wants to basically make some assertions
on this information, and this is basically in a text format.
It's by default in Postman not possible
to do anything with CSV.
So basically, want to see something like,
if this client, ABC1234 actually had an invoice
that was 3,500, for example.
This is how the response of the API looks,
and there are a couple of things we need to do
in order to get to this to work.
And obviously, because Postman doesn't offer any possibility
of parsing this information,
we need to rely on a third-party JavaScript library
to do the parsing for us.
One very popular and well-documented JavaScript library
that can do CSV parsing is Papa Parse,
and you can find it at papaparse.com.
They also have a GitHub account,
and here you'll find basically a very good documentation
with a lot of use cases on how to parse CSV files.
So basically, what we will need to do
is to somehow integrate this library in Postman.
And in order to do that, I will go to GitHub.
This is the repository of this project.
And the file that we are actually looking for
is this papaparse.min.js.
This is basically a minified file of the entire library.
You can see here that it's only one line.
And what we'll do in this case, I'm gonna click here on raw,
and I will copy the address that was given to me.
Now, in Postman, I created here a setup
that basically does this thing.
It calls this address that I've showed you previously
from GitHub, grabs the latest version of this library,
and then in the test tab, all I had to do
is to set a global variable called Papa Library.
Now, if this is a global variable or an environment variable
or what kind of variable you like,
there's makes absolutely no difference.
It's just important that the response
that I get from this library, which looks like this,
and it's called by the variable response body,
will be set to a global variable.
And that is quite easy to see,
because if I'm going to inspect variables
that I already have, I will see here that Papa Library
contains this long string here.
Now, this is the first step.
We just grabbing this library,
but still at this point, there's nothing happening.
So let's move on to the next request
that I have prepared for you,
and we're gonna switch to the test tab.
And what's happening here is that,
in one request, in the setup request,
I saved the library once, and now have the possibility
of reusing it in other location, other requests.
And in order to do that, we're gonna take advantage
of very powerful but very dangerous function as well
in JavaScript, eval function,
which evaluates a string and basically executes this code,
makes the code that we downloaded and saved as a string,
in this global variable,
it activates it so that we can use it.
And this will be available under this.Papa.
You can use this format,
or if you want to make it easier for you
when you're reading documentation,
you can just define the variable Papa
and assign it to this.Papa, and it would look exactly like
in the documentation of the library.
Now, this is the setup part, and you have to do this
in every request that you are planning to do CSV parsing.
The second step is to create a constant
with the Papa configuration.
In this case, I'm instructing Papa
to parse the header as well,
because as you have seen in the request, in the response,
there's given a header in this case.
There are a lot of other use cases
or a lot of other properties you can configure
all documented in the Papa documentation,
and all you have to do is to change this object
and add the properties
to the respective values that you need.
Now, this is basically the interesting part.
This is where the body will be parsed,
and so the response body will be parsed,
and the function that we're calling is Papa.parse,
so we're calling the parse function on the Papa object.
And we'll get a parse body,
and parse body is basically a JSON object
which is basically the same output,
a very similar output to the one that Postman generates you
when you're parsing a JSON format.
And after that, basically,
working with this object is pretty easy.
Now, if I introduced here a console.log as well
so that you can have a look at it,
all the information that has been parsed is inside data.
And here below is a simple test
on how you can actually test that this worked properly,
and in this case, we want to make sure
that client ABC123 was charged properly,
and for that reason,
we are gonna iterate over the response body.
So data will contain an array where each item in the array
is one line from the comma-separated value list.
And basically, we're gonna iterate it
using this construct, so for.
We're gonna initialize a variable called invoice,
and this will hold the current information,
so when you're iterating over the array,
invoice will always have the current value.
Actually, parsed body data is not really relevant.
You can replace that with invoice
so that I can easily see what's the current invoice
in case you're doing some debugging.
And then once I have an invoice,
I will match a specific value,
and only if this value is matched,
I will do an expectation here below.
So basically I'm expecting, after iterating all the values,
when I find a client called ABC1234,
I'm expecting that invoice total will be 4,500.
Now, I actually included here a value
that's a bit higher to make the test fail,
because I think it's always important to first make sure
that your tests fail when you're writing them
and then to go and fix them.
And last but not least,
because we are using a global variable,
I usually like, after running a test collection,
just to clear things up
so that I don't have any global variables hanging around
with information that I don't need.
It really depends on your workflow.
You can put it in this request,
or you have a request at the end
if you have multiple requests dealing with CSV formats.
It's totally up to you.
It's a thing that I like to do.
Okay, so let's hit the send button on this request,
and you'll see here that this is a response that I got.
And fortunately, this test is actually failing
because I inserted the wrong value,
and let me quickly fix you just to demonstrate
that the test is working as expected.
Of course, I'm getting this error now
that cannot read property parse of undefined.
This is totally fine, because once I've cleaned things up,
I need to run the setup again.
So I will quickly run the setup again,
go back to the solution, execute the test,
and voila, everything is working fine.
Now, the disadvantage of this solution
is that you're basically relying on an external dependency,
and of course, you can go to GitHub
and go to a specific version,
but every time there's something changing on GitHub
or changing on the library, you have a dependency on that.
So you have to take this in account
wherever you're rolling this test.
Ideally, I would just make a copy of this library
or something like that to store it on a local server
or whatever you think it's a fit for your scenario,
but just as well, we can just go to a specific version,
to a specific tag, and fetch it from there.
This is one way on how you can deal
with CSV responses in Postman.
I'm not saying this is the only way how you can do it,
but you have to rely on a third-party library
that you need to include in your tests
in order to make it possible.
Now, Papa Parse is not the only library
that can parse CSV files.
There are a lot of them.
You can search them on GitHub or on Stack Overflow
or whenever you're searching for such library.
I personally liked it a lot because it's easy to use,
it has a clear documentation, it's clear it's very popular,
and it has a large user base.
So for that reason, I will highly recommend this one.
Hope you find this tutorial helpful,
and I'm really looking for feedback
or any other questions that you have on Postman
or any other interesting problems that you're dealing with.
Just let me know, write a comment below,
and maybe I'll do a video on them.
Make sure you check the video description,
because there I'll be posting a link
to the collection that I've used in this example,
and you will find other resources as well.
And see you next time at another tutorial.
(upbeat music)