Endpoint downloads PDF. How to do this via Collection Runner or Newman?

Hey @russellg, :wave: I was able to read and write a PDF file as follows:

Created a request fetching a PDF file:


In the โ€œTestโ€ script Iโ€™m sending the raw response stream to a locally running server.

localhost/save is the endpoint of the server that Iโ€™m running.
pm.response.stream is the data I sent to this server.

Iโ€™m running a PHP script at the local server to read the data and write it to a file.
This is how my server-side PHP script looks like

<?php 
   
$FP = fopen('./saved.pdf','w+') or die("Error creating PDF");

$outp = fopen('php://input', 'r+') or die("Error reading data");


// pm.response.stream is a JSON object so we decode it first
$json = json_decode(stream_get_contents($outp));

// PDF data is inside the 'data' field
// All bytes are stored as an integer
$data = $json->data;
for($idx = 0; $idx < count($data); ++$idx) {
      // covert the bytes to characters (binary) and write to the file
      fwrite($FP,chr($data[$idx])) or die("Error writing data");
}

fclose($outp);
fclose($FP);

echo "Written!";

I was successfully able to open the saved.pdf file.

You will have to see how this can be done in the language of your choice!

Hope this helps :+1:

2 Likes