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.
CodePudding user response:
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"]}
CodePudding user response:
Convert the CSV file resolved my issue. $base64string = [Convert]::ToBase64String([IO.File]::ReadAllBytes("MyCSVFile.csv"))