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"