简体   繁体   English

Powershell API - 在变量内部传递变量

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.我正在努力解决如何在我的 API function 中传递 base64 编码。 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")) $base64string = [转换]::ToBase64String([IO.File]::ReadAllBytes("D:\Temp\MyJSON.txt"))

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Content-Type", "application/json") $headers = 新对象 "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Content-Type", "application/json")

$body = "{ "token ": "Token ", "file ": "data:text/csv;base64,$base64string "]}" $body = "{ "token ": "Token ", "file ": "data:text/csv;base64,$base64string "]}"

$response = Invoke-RestMethod 'baseurl + endpoint' -Method 'POST' -Headers $headers -Body $body $response | $response = Invoke-RestMethod 'baseurl + endpoint' -Method 'POST' -Headers $headers -Body $body $response | ConvertTo-Json转换为 JSON

Issue: I'm not able to pass $base64string in $body variable.问题:我无法在 $body 变量中传递 $base64string。 It does works if I replace $base64string with the actual strings.如果我用实际的字符串替换 $base64string 它确实有效。

2 个回复

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.转换 CSV 文件解决了我的问题。 $base64string = [Convert]::ToBase64String([IO.File]::ReadAllBytes("MyCSVFile.csv")) $base64string = [转换]::ToBase64String([IO.File]::ReadAllBytes("MyCSVFile.csv"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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