How to handle CSV response with postman

Hello all,

I am trying to process a CSV response, and verify values within the CSV returned are matching what I expect based on the beginning of my collection. Can anyone point me in the right direction on how I might go about doing this?

Thanks,
Grant

Can you post a minimal sample of your CSV response and what kind of things you are trying to assert?

@vdespa certainly! Here’s a sample of the CSV I’m receiving:

InvoiceId,InvoiceCreate,InvoiceCapture,First,Last,InvType,InvTotal,InvStatus,InvCredit,ResId,ResHash,ResStart,ResEnd,Duration,Decal,Plate,Market,Community,TollId,BeforeTax,Tax,AfterTax
34001,2018-03-07T21:46:30+0000,2018-03-07T21:46:37+0000,First,Last,Test,β€œ$3,500.00”,PAID,N/A,1234,ABC1234,2018-03-09T14:00:00+0000,N/A,API-GAP12,β€œAPI-RSQ - 853”,β€œTest Data”,β€œTest Data”,7712141,$92.42,$9.24,$101.66
34000,2018-03-07T20:39:46+0000,2018-03-07T20:39:46+0000,First,Last,Test,β€œ$3,500.00”,PAID,N/A,1234,CDE1234,2018-03-12T13:00:00+0000,N/A,ABCD123ID,ABCD123,Test,Data,123ABCD,$0.00,$0.00,$0.00
33999,2018-03-07T20:25:53+0000,2018-03-07T20:25:56+0000,First,Last,Test,β€œ$3,500.00”,PAID,N/A,1234,EFG1234,β€œTest Data”,N/A,β€œ$1,363.64”,$136.36,β€œ$1,500.00”
33998,2018-03-07T20:08:53+0000,2018-03-07T20:08:56+0000,First,Last,Test,β€œ$3,500.00”,PAID,N/A,1234,GHI1234,β€œTest Data”,N/A,β€œ$1,501.50”,$298.50,β€œ$1,800.00”

Trying to assert whether something appears in the output, with the correct fee. So for the example above I might be looking to see that ResHash ABC1234 was charged the correct amount of 3500.

UPDATE: See answer from @saswat.das first.

Interesting problem, so I decided to do a video on this:

Below a short summary:

  1. You can use a 3rd party library to parse a CSV response
  2. I suggest using papaparse.com
  3. After parsing the response, you have a JavaScript object, so nothing special anymore.

Let me know if this helps you.

2 Likes

Postman script has csv-parse library built in.

1 Like

Hi @grant.prichard, Postman natively supports CSV parsing. Here is a sample test script that does exactly what you asked for

Trying to assert whether something appears in the output, with the correct fee. So for the example above I might be looking to see that ResHash ABC1234 was charged the correct amount of 3500.

Postman - Test Script

/* 
 *   For documentation check http://csv.adaltas.com/parse/
 *   and https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference
 *   
 */

const parse = require('csv-parse/lib/sync'),
    parsedBody = parse(responseBody, {relax_column_count: true});
    
pm.test("Correct CSV response", function () {
    // Check that we have at least 2 two (header + row)
    pm.expect(parsedBody).to.have.length.above(1);
    
    // Getting the index of ResHash and InvTotal 
    const head = parsedBody.shift(),
        resHash = head.indexOf('ResHash'),
        invTotal = head.indexOf('InvTotal');
    
    // Iterate through the rows
    parsedBody.forEach(function(row) {
        
        // If resHash matches the required value perform the test
        if(row[resHash] === 'ABC1234') {
            pm.expect(row[invTotal]).to.equal('$3,500.00');
        }
    })
});

I am also sharing a postman collection with you, which you can use to get started https://www.getpostman.com/collections/4fe24427914d24149c8e

Also feel free to reach out to us, If you need any more help or clarification regarding CSV parsing in Postman

2 Likes

this worked like a charm, thank you so much for the help!