Powershell 如何使用包含文件数据的JSON正文发送Invoke WebRequest

Powershell 如何使用包含文件数据的JSON正文发送Invoke WebRequest,powershell,invoke-webrequest,Powershell,Invoke Webrequest,问题: 如何将文件内容放在Invoke WebRequest的JSON主体中,而不使用不需要的文件元数据 我的目标是发送一个HTTP请求,如下所示: Invoke-WebRequest -Uri http://localhost:4321/updatefile ` -ContentType 'application/json' ` -Method POST ` -Body $Body 其中: PS C:\Users\User1234> $Body = Conver

问题:
如何将文件内容放在Invoke WebRequest的JSON主体中,而不使用不需要的文件元数据

我的目标是发送一个HTTP请求,如下所示:

Invoke-WebRequest -Uri http://localhost:4321/updatefile `
    -ContentType 'application/json' `
    -Method POST `
    -Body $Body
其中:

PS C:\Users\User1234> $Body = ConvertTo-Json @(
    @{filename='file1.txt';filecontent=$file1},
    @{filename='file2.txt';filecontent=$file2}
)

PS C:\Users\User1234> $file1 = Get-Content "C:\path\to\file1.txt"
PS C:\Users\User1234> $file2 = Get-Content "C:\path\to\file2.txt"
当我打印变量时:

PS C:\Users\User1234> echo $file1
aaaaa
PS C:\Users\User1234> echo $file2
bbbbb
…它按我的预期打印文件内容。
但是在
$Body
中打印文件内容会显示更多我不需要的信息:

PS C:\Users\User1234> echo $Body
{
    "filename":  "file1.txt",
    "filecontent":  {
                        "value":  "aaaaa",
                        "PSPath":  "C:\\path\\to\\file1.txt",
                        "PSParentPath":  "C:\\path\\to",
                        "PSChildName":  "file1.txt",
                        "PSDrive":  {
                                        "CurrentLocation":  "Users\\User1234",
                                        "Name":  "C",
                                        "Provider":  "Microsoft.PowerShell.Core\\FileSystem",
                                        "Root":  "C:\\",
                                        "Description":  "OS",
                                        "MaximumSize":  null,
                                        "Credential":  "System.Management.Automation.PSCredential",
                                        "DisplayRoot":  null
                                    },
                        "PSProvider":  {
                                           "ImplementingType":  "Microsoft.PowerShell.Commands.FileSystemProvider",
                                           "HelpFile":  "System.Management.Automation.dll-Help.xml",
                                           "Name":  "FileSystem",
                                           "PSSnapIn":  "Microsoft.PowerShell.Core",
                                           "ModuleName":  "Microsoft.PowerShell.Core",
                                           "Module":  null,
                                           "Description":  "",
                                           "Capabilities":  52,
                                           "Home":  "C:\\Users\\User1234",
                                           "Drives":  "C D Y"
                                       },
                        "ReadCount":  1
                    }
}
我尝试使用以下方法设置
$file1
$file2
值:

$file1 = [IO.File]::ReadAllText("C:\path\to\file1.txt")    
$file2 = [IO.File]::ReadAllText("C:\path\to\file2.txt")

…但结果是一样的。

首先设置$body,然后转换为json

$body = @(
    @{
        filename = 'file1.txt'
        filecontent = [io.file]::ReadAllText("1.txt")
    }
)

$body | ConvertTo-Json

输出

{
    "filename":  "file1.txt",
    "filecontent":  "1\r\n2\r\n3\r\n"
}

原因很简单,
get content
返回一个数组,
convertto json
完成了它的工作,但可能不是您所期望的。

答案的可能重复是有效的,但请注意,它工作的唯一原因是使用
[io.file]::ReadAllText()
而不是
Get Content
绕过了
Get Content
ConvertTo Json
包含在序列化中的元数据装饰输出字符串的问题-请看,这也不是因为
Get Content
返回一个行数组:通过使用
-join“`r`n”
通过创建新字符串,您再次绕过了问题。获取整个文件内容的首选方法是使用
get Content-Raw 1.txt
,但如果使用该方法,则会重新出现额外属性的问题。
{
    "filename":  "file1.txt",
    "filecontent":  "1\r\n2\r\n3\r\n"
}