‫Industry, albeit 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.
‫As I was telling in the introduction, I wanted to show in this video how to handle these three responses
‫with Postman.
‫Now, we have here a very clear example that came from Greg, 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 we want to see something like if this client ABC one, two, three, four actually had
‫an invoice that was 3500, 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 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.
‫We're very popular and well documented.
‫JavaScript library that can do CSV parsing is pop up parse and you can find it at pop up cars.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 these V files.
‫So basically what we will need to do is to some somehow integrate this library in Postman.
‫And in order to that, I will go to GitHub.
‫And this is the repository of this project and the file that we're actually looking for is this pop
‫up parts that mean that, geez, 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 going to click here on
‫Raw and I will copy the address that was given to me.
‫Now in post man.
‫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 a 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'll see
‫here that a library contains this long string here.
‫Now, this is the first step.
‫We just grabbing this library.
‫But still, at this point, there is nothing happening.
‫So let's move on to the next request that I have prepared for you, and we're going to 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 I have the possibility of reusing it in other location, other requests.
‫And in order to do that, we're going to take advantage of very powerful but very dangerous function
‫as well in JavaScript evolve function which evaluates a string and basically executes this code, makes
‫the code that we just 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 paper.
‫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 will look exactly like in the
‫documentation of the library.
‫Now this is the setup part and you have to do it in every request that you're planning to do csv parsing.
‫The second step is to create a constant which the proper configuration.
‫In this case I am instructing Papa to pass the header as well because as you have seen in the request
‫in response, there is 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 purported to parse.
‫So we're going to parse function on the paper object and we'll get to parse body.
‫And this is parse body is basically adjacent object, which is the same basically the same output.
‫A very similar output to the one that postman generates you when you're passing adjacent format.
‫And after that, basically working with this object is pretty easy.
‫Now I've introduced here a console lock as well, so that you can have a look at it.
‫All the information that has been parsed in 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 ABC one, two, three was charged properly.
‫And for that reason, we are going to 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 going to iterate it.
‫Using this construct.
‫So for we can initialize an 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 passed.
‫Body data is not really relevant.
‫We can replace that with invoice, but 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 specific value and only if this value is matched.
‫I'll do an expectation here below.
‫So basically I'm expecting after iterating all the values when I find a client called ABC one, two,
‫three, four, I'm expecting that the invoice total will be 4500.
‫Now 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're 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 will see here that this is a response that I got.
‫And fortunately, this state, this test is actually failing because I inserted the wrong value.
‫And let me quickly fix you this to demonstrate that the test is working as expected.
‫Of course I'm getting this error now that cannot read properly parts of undefined.
‫This is totally fine because once I 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 into account whenever you're wherever you're rolling these tests.
‫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, paper path is not the only library that can pass CSV files.
‫There are a lot of them you can search them on GitHub or StackOverflow or whenever you are searching
‫for such library.
‫I personally liked it a lot because it has a 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.
‫I hope you find this tutorial helpful and I'm really looking for feedback or any other questions that
‫you have on post 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.
‫Sure, you check the video description because there will be posting a link to the collection that I've
‫used in this example.
‫And you will find other resources as well.
‫See you next time at another tutorial.