0

I'm looking to execute the following POST command in powershell, passing in the body as the parameter

$request = Invoke-Restmethod -TimeoutSec 600 -URI 'http://<servername>:5000/api/test/test' -Method POST -Body "aum_date_str:2020-09-16" -ContentType "application/json"

but it fails with an invalid json format error ...

Invoke-Restmethod : {
  "type": "about:blank",
  "title": "Bad Request",
  "detail": "Request body is not valid JSON",
  "status": 400
}

I have verified the json format and it's fine. Any ideas why this error is occurring?

大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
BobbyS
  • 49
  • 2
  • 8
  • Try `$request = Invoke-Restmethod -TimeoutSec 600 -URI 'http://:5000/api/test/test' -Method POST -Body '{"aum_date_str":"2020-09-16"}' -ContentType "application/json"` – CraftyB Sep 17 '20 at 13:01
  • Sorry for the delay but thanks very much. I made a few tweaks and got it working after looking at the previous answer that Kyle posted. – BobbyS Sep 17 '20 at 15:10

2 Answers2

0

Your -Body-parameter is not a JSON. Correct JSON-document must begins and ends with braces ({}) and field name and value must be in quotes. Value may be without quotes if it is integer or boolean.

Try to send this: {"aum_date_str":"2020-09-16"}

More about JSON syntax you can read on Wikipedia or here

Indian
  • 529
  • 1
  • 12
  • 25
  • Same error with this as well unfortunately. – BobbyS Sep 17 '20 at 09:46
  • I've also added $param = "aum_date_str:$Date" (with the $Date variable being 2020-09-16), and then added ($param|ConvertTo-Json) to the request, but now I'm getting an error saying the parameter I'm using "is not of type object" – BobbyS Sep 17 '20 at 09:48
  • @BobbyS, check updated version. I've add quotes to JSON field and value – Indian Sep 17 '20 at 09:52
  • Sorry for the delay but thanks very much. I made a few tweaks and got it working after looking at the previous answer that Kyle posted. – BobbyS Sep 17 '20 at 15:10
0

maybe you should add {}

$request = Invoke-Restmethod -TimeoutSec 600 -URI 'http://:5000/api/test/test' -Method POST -Body "{aum_date_str:2020-09-16}" -ContentType "application/json"

Sean Lee
  • 1
  • 1
  • Hi! I tried this but still getting the same error – BobbyS Sep 17 '20 at 09:45
  • Sorry for the delay but thanks very much. I made a few tweaks and got it working after looking at the previous answer that Kyle posted. – BobbyS Sep 17 '20 at 15:11
  • You will need the quotes to make the json valid $json = @" { "aum_date_str": 2020-09-16 } "@ $request = Invoke-Restmethod -TimeoutSec 600 -URI 'http://:5000/api/test/test' -Method POST -Body $json -ContentType "application/json" – Jeno Laszlo Sep 18 '20 at 08:33
  • '{"aum_date_str": 2020-09-16}' or "{`"aum_date_str`": 2020-09-16}" should work as well – Jeno Laszlo Sep 18 '20 at 08:34