Extract attached file from response

876 Views Asked by At

According to Whitesource document, the response headers will have

Content-Type = application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Content-Disposition: attachment; filename=<product name>.xslx 

I want to extract that xslx file but, I do not know how. I've tried writing the response in a file but, all I got is is a bunch of binary characters.

Invoke-RestMethod -SkipCertificateCheck -Method Post -ContentType 'application/json' -Body $body -Uri "https://app.whitesourcesoftware.com/api/v1.3 | Out-File "abcd.csv"

I also tried to convert the response to csv before writing it, but that doesn't work either.

Invoke-RestMethod -SkipCertificateCheck -Method Post -ContentType 'application/json' -Body $body -Uri "https://app.whitesourcesoftware.com/api/v1.3 | ConvertTo-CSV | Out-File "abcd.csv"

Any idea?

1

There are 1 best solutions below

0
7_R3X On BEST ANSWER

After a little digging, I realized that the file is present in the content part of the respose. I used Invoke-WebRequest instead of Invoke-RestMethod. Here's my scripts:

$response = Invoke-WebRequest -Method Post -ContentType 'application/json' -Body $body -Uri $server
[System.IO.File]::WriteAllBytes("report.xlsx", $response.content)

This will write the attached file to a file named report.xlsx.