简体   繁体   中英

Extract attached file from response

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM