0

My Api accepts object as a paramenter and I wanted to pass it from powershell. I have copied the powrshell code. Please advise as when the api is invoked null value comes into api as input.

Class JsonArray
{

    [String]$json
}

  $Json ='hi'

  $jsonArray = [JsonArray]::new()
  $jsonArray.json = $Json

  $params = $jsonArray


Invoke-WebRequest -Uri http://localhost:65452/api/e10/e10PostTCData/ Method Post -Body $params -TimeoutSec 600

Below is my API

    [Route("e10PostTCData/")]
    [HttpPost]
    public HttpResponseMessage PostResults(JsonArray jsonArray )
    {

    }

    public class JsonArray
    {
       public string json { get; set; }
    }
Shreyas Murali
  • 329
  • 1
  • 2
  • 17
  • [Possible duplicate](https://stackoverflow.com/questions/35722865/making-a-powershell-post-request-if-a-body-param-starts-with).... – Peter Schneider Apr 12 '19 at 07:07
  • I have updated my question along with the API, please advise. – Shreyas Murali Apr 12 '19 at 07:53
  • I guess you're missing the [FromBody] attribute.. See here for detailed documentation of [Parameter Binding in WebAPI](https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api)... – Peter Schneider Apr 12 '19 at 08:25
  • The same API works when called from Angular but not from powershell, Is [FromBody] mandatory when called from powershell? – Shreyas Murali Apr 12 '19 at 09:43
  • Your `$json` variable doesn't contain JSON, and PowerShell class instances don't serialize to JSON. I have no idea what your API needs, but try `$params = @{ 'json'='hi' }` – TessellatingHeckler Apr 12 '19 at 09:52
  • I formatted the test to JSON and placed [FromUri] into my web api and it worked. Invoke-WebRequest -Uri http://localhost:65452/api/e10/e10PostTCData/?json=$Json -Method Post -Body $params – Shreyas Murali Apr 12 '19 at 10:01
  • In this case your passing the$Json twice... You can omit the -Body $params then... Also take a look at the invoke-restmethod cmdlet, which is better in your usecase.. – Peter Schneider Apr 12 '19 at 17:23

1 Answers1

0
[String]$Json =@{ 'json'='hi' }

$params = $Json

Invoke-WebRequest -Uri http://localhost:65452/api/e10/e10PostTCData/?json=$Json - 
Method Post -Body $params


    [Route("e10PostTCData/")]
    [HttpPost]
    public HttpResponseMessage PostResults([FromUri]JsonArray jsonArray )
    {
    } 

   public class JsonArray
   {
     public string json { get; set; }
   }
Shreyas Murali
  • 329
  • 1
  • 2
  • 17