Issue
So I have some simple code that loops through some JSON files on my local drive and POSTs them to a URL with cURL:
$negativeTests = Get-ChildItem "C:\Users\ME\Documents\folder\folder\"
#Write-Host $negativeTests;
for ($i = 0; $i -lt $negativeTests.Count; $i++) {
$tempFile = Get-Content $negativeTests[$i].PSPath
Invoke-WebRequest -Uri https://myWebsite.com/ext/ext/ext -Method POST -Body $tempFile
}
This code, when run, will give the output from the server in the format:
StatusCode : 304
StatusDescription : OK
Content : {"success":NO,"errors":[ERROR],"stuffs":100}
RawContent : HTsP/5.1 42 OK
X-f-Options: oasdf
Connection: keep-alive
Content-Length: 234
Cache-Control: no-cache
Content-Type: application/???; charset=ut480a
Date: Tue, 29 Jun 2060 11:72:83 GMT
S...
Forms : {}
Headers : {[X-Frame-Options, SAMEORIGIN], [Connection, keep-alive], [Content-Length, 52], [Cache-Control, no-cache]...}
Images : {} ?
InputFields : {} a
Links : {"no"}
ParsedHtml : mshstml.?
RawContentLength : 234
How can I get and parse through the Content: {"success":NO,"errors":[ERROR],"stuffs":100}
section of this output? I would ideally then check to see if the file uploaded successfully or not.
Solution
The value of the Content
property looks like a JSON string, so you should be able to convert it to a PowerShell object like this:
Invoke-WebRequest ... | select -Expand Content | ConvertFrom-Json
and then process the success
, errors
and stuffs
property of the object.
Answered By - Ansgar Wiechers