0

I'm attempting to make a POST request to a local java spring application via PowerShell.

I load the request from a file, convert it to json, then attempt to Invoke-RestMethod.

$filePath = some/file/path.json
$string = [System.IO.File]::ReadAllText($filePath)
$body = $string | ConvertTo-Json
$cred = Get-Credential
$uri = "some:url/restUri"

$response = Invoke-RestMethod -Method 'Post' -Uri $uri -Credential $cred -Body $body -ContentType "application/json"

After running this I get an IOException followed by a 'connection forcefully closed by remote host'. It says that there isn't a constructor or objectfactory that can serialize the java object form the string...

when I echo $body it looks fine (although the request is huge), but the kicker is that if I just ctrl+a ctrl+c the text file and paste it into postman and run it, the service is successful.

Just to be sure I copied the request from postman and put it back into the file, reloaded the file into the variable and converted it to json and tried again; same issue.

The request is massive even after removing the nullable fields, but from what I can see in powershell by echoing the request it looks fine...

Is there something about reading files in powershell that might add hidden characters to the variable, and/or is there an issue with sending huge requests via powershell?

UPDATE: If I remove the bulk of the request, leaving a valid json entity but one which fails to match the schema, I get a 400 error (Bad Request).

UPDATE 2: I found out how to get the json message as the application attempts to process it (I thought the process of converting the string to json might be the problem). I copied the json the application claimed was problematic and diff'd it to the file containing the actual json - they're identical.

Wickerbough
  • 365
  • 1
  • 4
  • 15

1 Answers1

0

Figured it out: Apparently the ConvertTo-Json step was the problem. I'm not exactly sure why it was causing an issue, but when I send $content (the contents of the file, directly copied as text) the invocation works, but if I set $body = $content | ConvertTo-Json and send $body, the request fails.

EDIT: Can't mark this as an answer for 2 days apparently.

Wickerbough
  • 365
  • 1
  • 4
  • 15