I'm strugling on how to pass a base64 encode inside my API function. It works if I pass manual strings. But I want to dynamic strings.
Here is my code:
$base64string = [Convert]::ToBase64String([IO.File]::ReadAllBytes("D:\Temp\MyJSON.txt"))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Content-Type", "application/json")
$body = "{ "token
": "Token
", "file
": "data:text/csv;base64,$base64string
"]}"
$response = Invoke-RestMethod 'baseurl + endpoint' -Method 'POST' -Headers $headers -Body $body $response | ConvertTo-Json
Issue: I'm not able to pass $base64string in $body variable. It does works if I replace $base64string with the actual strings.
Doublequote everything inside the outter quotes
$body = "{"token":"Token","file":"data:text/csv;base64,$base64string"]}"
$body = "{""token"":""Token"",""file"":""data:text/csv;base64,$base64string""]}"
$base64string = "test"
$body = "{""token"":""Token"",""file"":""data:text/csv;base64,$base64string""]}"
Write-Output $body
{"token":"Token","file":"data:text/csv;base64,test"]}
Convert the CSV file resolved my issue. $base64string = [Convert]::ToBase64String([IO.File]::ReadAllBytes("MyCSVFile.csv"))
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.