当从powershell调用api时,Null值来自Post方法(FromBody)

问题描述 投票:2回答:2

从PowerShell调用API时,Post方法参数为null。下面是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包含上面的json数组。 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; }
 }

另一种方式:

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

}

两种方法都显示null作为参数。请指教。

c# api powershell web
2个回答
1
投票

在这样的形式中,您的服务期望在顶级接收json和json字段,与您的类JsonArray.json字段相同。所以在这种情况下,您的请求正文应该是这样的:

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

如果要传递JSON主体,则需要具有与JSON具有相同字段的类具有键。

否则,您可以将JSON读作字符串,转换为JSON并手动处理,如:

   [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();
   }

对于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":""
    }   
}

响应

TestCase1

更新:如果要将JSON作为字符串参数接收到端点:

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

那么你的请求应该是一个多行字符串,而不是一个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":""
    }   
}'

更新:我不确定PowerShell的所有细节,但以下对我有用:

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

0
投票

如果json数据在其正文中发送,则Powershell API请求应如下所示:

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

© www.soinside.com 2019 - 2024. All rights reserved.