So I have this endpoint that is used to download a PDF. I can click Send and Download and if I save the response with the PDF extension, it writes to the file.
Is there anyway I can have the PDF created using the Collection runner or Newman? So that if instead of just sending the request it Sends and Downloads as well?
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!
Hi @danny-dainton, can I do this on the fly? I don’t want to save the file. I will just get the stream for pdf from a service, and post it to another service.
I appreciate any help.
Sure. Let me explain step by step.
1 - I can GET a pdf file from a webservice using postman. When I click SaveResponse button on the UI the pdf is saved to local and opens without any problem.
2 - I can POST the local pdf file to another webservice using postman. When I select File option on Request/body/form-data Key field and select the file on value, post is done successfully and I can open the pdf file on the target without any problem.
I want to do these two steps in one collection. I don’t want to store the file on my local. I just want to get the file, save the content (stream) to an environment variable, and post this content to the other webservice. Is this possible either Postman or Newman?
When I try to do it, the pdf file posted is created empty!
It’s not something that I’ve tried but it might be possible to script something similar to the solution above, that saves the PDF and then reference that filename in the collection for the POST request and have newman pick it up during the run.
This is how file uploads work in Newman:
Like I said, I’ve never done this some I wouldnt know for sure if it would work