How to send Invoke-WebRequest with JSON body containing data from File How to send Invoke-WebRequest with JSON body containing data from File powershell powershell

How to send Invoke-WebRequest with JSON body containing data from File


Setup $body first, then convert to json.

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

OR

$body = @(    @{        filename = 'file1.txt'        filecontent = (get-content 1.txt) -join "`r`n"    })$body | ConvertTo-Json

output

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

the reason is simple, get-content returns an array, convertto-json does its job but probably not you expected.