【问题标题】:Send python request to .net API向 .net API 发送 python 请求
【发布时间】:2022-01-14 23:02:19
【问题描述】:

我正在尝试将发布请求从 python 发送到 .net Core Api。但是我收到了

The JSON value could not be converted to NetflowInputViewModel.

我的视图模型是=

public class NetflowInputViewModel
    {
        public DateTime startDate { get; set; }
        public DateTime endDate { get; set; }
        public int ipv6 { get; set; }
    }

我的控制器是 =

[HttpPost]
        [Route("CdrDetails")]
        public JsonResult CdrDetails([FromBody] NetflowInputViewModel model)
        {
            List<NetflowOutputViewModel> netFlow = netflowCdrService.GetDetails(model.startDate, model.endDate, model.ipv6);
            if (netFlow == default)
            {
                return new JsonResult("No Records.");
            }
            else
                return new JsonResult(netFlow);
        }

我正在尝试从 python 发送参数,像这样 =

import json
from datetime import date, datetime
import requests

def json_serial(obj):
    if isinstance(obj, (datetime, date)):
        return obj.isoformat()
    raise TypeError("Type %s not serializable" % type(obj))

url = "https://localhost:44388/api/NetflowCdr/CdrDetails"

start_date = datetime(2021, 11, 29, 15, 10, 0)
end_date = datetime(2021, 11, 29, 15, 20, 0)
myobj = {"startDate": start_date, "endDate": end_date, "ipv6": 0}
aa = json.dumps(myobj, indent=4, sort_keys=True, default=str)
x = requests.post(url, json=aa, verify=False, headers={
    "Content-Type":"application/json", "Accept": "*/*"
})
print(x.text)

我该如何处理?我想我应该在 .net 核心方面改变一些东西,但我不知道该怎么做

【问题讨论】:

    标签: python json .net api


    【解决方案1】:

    不确定这是否有帮助,但我认为这可以帮助您 在 .net core 3.0 Newtonsoft.JSON 包中,所以你可以试试这个

    Install Microsoft.AspNetCore.Mvc.NewtonsoftJson package
    

    在你的 startup.cs 添加

    services.AddControllers().AddNewtonsoftJson();
    

    这可能会对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-16
      • 2019-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      相关资源
      最近更新 更多