从Powershell调用api时,空值来自Post方法(FromBody)

时间:2019-04-15 15:12:16

标签: c# api powershell web

从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-方法发布-正文$ 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用作参数。请告知。

2 个答案:

答案 0 :(得分:1)

在这种形式的服务中,您的服务希望在顶层收到 $url = "http://ip-api.com/batch"; $headers = array('Content-Type: application/json'); $fields = '[{"query": "208.80.152.201"}, // You can add multiple IP address in the same format. {"query": "8.8.8.8"}, {"query": "24.48.0.1"}]'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POSTFIELDS, $fields); $httpCode = curl_getinfo($curl , CURLINFO_HTTP_CODE); $response = curl_exec($curl); if ($response === false) $response = curl_error($curl); foreach(json_decode($response) as $value){ echo $value->lat; //Use the result to store the information in DB } curl_close($curl); 字段的json,就像您的类json字段一样。因此,在这种情况下,您的请求正文应类似于:

JsonArray.json

如果要传递JSON正文,则需要具有一个与JSON具有键相同的字段的类。

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

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

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

响应

{
    "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作为端点的字符串参数:

TestCase1

然后,您的请求应为多行字符串,而不是JSON,这是非常不便的,除非您是端点的唯一用户。例如:

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

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

'{
    "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":""
    }   
}'

答案 1 :(得分:0)

如果JSON数据在其主体中发送,则Powershell API请求应类似于以下内容:

调用WebRequest-Uri http://localhost:65452/api/e10/e10PostTCData-方法发布-正文= $ json