2
votes

Post method parameter is taken as null when API is invoked from powershell. Below is the JSON

 "TestCase":{
    "tc_name":"TestCase1"
    },
    "3":{
    "scn_desc":"Create Client34345",
    "test_status":"PASS",
    "error_link":""
    }   ,
  "4":{
    "scn_desc":"Create Client43634",
    "test_status":"PASS",
    "error_link":""
    },
  "5":{
    "scn_desc":"Create Client346346",
    "test_status":"PASS",
    "error_link":""
    }   
  }

$json contains the above json array. Powershell: Invoke-WebRequest

-Uri http://localhost:65452/api/e10/e10PostTCData -Method Post -Body $json -ContentType 'application/json'

API:

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

 }

 public class JsonArray
 {
   public string json { get; set; }
 }

Other way:

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

}

Both the methods shows null as the parameter. Please advise.

2
Yes. But when passed to API the parameter comes as null in APIShreyas Murali

2 Answers

1
votes

In form like this your service expects to receive json with json field on top level, same like your class JsonArray.json field. So in this case your request body should be something like:

{
    "json": "Whatever text you need to pass here"
}

If you want to pass your JSON body, you need to have a class that has all the same fields as your JSON has keys.

Otherwise you can read your JSON as string, convert to JSON and process manually, like:

   [Route("e10PostTCData")]
   [HttpPost]
   public async Task<string> PostResults()
   {
       var bytes = new byte[(int)Request.ContentLength.Value];
       await Request.Body.ReadAsync(bytes, 0, bytes.Length);

       var content =  Encoding.UTF8.GetString(bytes);
       var json = JObject.Parse(content);

       return json["TestCase"]["tc_name"].ToString();
   }

For json body:

{
    "TestCase":{
        "tc_name":"TestCase1"
    },
    "3": {
        "scn_desc":"Create Client34345",
        "test_status":"PASS",
        "error_link":""
    }   ,
    "4":{
        "scn_desc":"Create Client43634",
        "test_status":"PASS",
        "error_link":""
    },
    "5":{
        "scn_desc":"Create Client346346",
        "test_status":"PASS",
        "error_link":""
    }   
}

Response

TestCase1

UPDATE: If you want to receive JSON as a string argument to your endpoint:

    public string PostResults([FromBody]string jsonStr)
    {
        // Use JObject class methods Parse or ToObject (deserialize) your string into object
        return jsonStr;
    }

Then your request should be a multiline string, NOT A JSON, which is very inconvenient unless you're the only user for your endpoint. For instance:

'{
    "TestCase":{
        "tc_name":"TestCase1"
    },
    "3": {
        "scn_desc":"Create Client34345",
        "test_status":"PASS",
        "error_link":""
    }   ,
    "4":{
        "scn_desc":"Create Client43634",
        "test_status":"PASS",
        "error_link":""
    },
    "5":{
        "scn_desc":"Create Client346346",
        "test_status":"PASS",
        "error_link":""
    }   
}'

UPDATE: I amm not sure about all specifics of PowerShell, but the following worked for me:

> $obj='{
      "TestCase":{
          "tc_name":"TestCase1"
      }
  }'
> Invoke-WebRequest http://localhost:65452/api/e10/e10PostTCData -Body $(echo $obj | ConvertTo-Json) -Method POST -ConventType "application/json"
0
votes

Powershell API request should be like below if the json data is sent in its body:

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