简体   繁体   中英

Powershell API - Passing a variable inside of a variable

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文简体   中文繁体   中英对照 版本,有任何建议请联系yoyou2525@163.com。

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.

2 anwsers

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.

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