【问题标题】:Null value comes from Post method(FromBody) when api is invoked from powershell从powershell调用api时,空值来自Post方法(FromBody)
【发布时间】:2019-09-05 14:46:31
【问题描述】:

从 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 数组。 电源外壳: 调用-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 作为参数。请指教。

【问题讨论】:

  • 是的。但是当传递给 API 时,参数在 API 中为 null

标签: c# api powershell web


【解决方案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"

【讨论】:

  • 另一种方式呢。 [Route("e10PostTCData/")] [HttpPost] public HttpResponseMessage PostResults([FromBody]string jsonArray ) { } 将完整的 json 作为字符串传递到 API 正文中。有什么办法吗?
  • 下面是我用来调用 API 的 powershell 脚本: $obj = "{""TestCase"":{" $obj = $obj + "n""tc_name"":TestCase1""" $obj = $obj + "}" $obj = $obj + "n""}" $ obj Invoke-WebRequest -Uri localhost:65452/api/e10/e10PostTCData -Method Post -Body $obj 但参数为null。 [Route("e10PostTCData/")] [HttpPost] public HttpResponseMessage PostResults([FromBody] string jsonArray ) { }
  • @ShreyasMurali,看看我的最新更新,应该会有所帮助。
  • 谢谢!下面的代码也应该可以工作。 Invoke-WebRequest-Uri localhost:65452/api/e10/e10PostTCData -Method Post -Body =$obj
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 2021-05-16
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多