How to pass a binary Response from a GET request to a POST request

Hello team,

I am trying to retrieve a PDF file from a REST API endpoint, and pass the binary response into another REST API endpoint in order to upload it there. For this I am using the Tests tab using the code below:

let response_data = pm.response.stream.toString('binary');
const postRequest = {
   url: 'https://server.com/api/rest/uploadDocument',
   method: 'POST',
   header: {
            'Authorization' : 'Bearer XXXX'
   },
   body: {
             mode: 'formdata',
             formdata: [
                          {key: "File", value: response_data},
                          {key: "File-Name", value: "test.pdf"},
                          {key: "Mime-Type", value: "application/pdf"},      
             ]
       }
};

 pm.sendRequest(postRequest, (error, response) => {
 console.log(error ? error : response.json());
});

While the POST request is successful, the file ends up being corrupted, and if I put a Fiddler trace I can see some of the binary characters are being replaced by question marks β€˜?’. Could this be an encoding issue? How can I retrieve the correct response and pass it to the new request without corrupting the data?

If I try to download the response using the β€œSave Response” button it works just fine, which means that the response is received successfully.

Many thanks,
Cedric

Hi Cedric,
did you find a solution?
Thanks.