【问题标题】:Get the POST data back on Web API在 Web API 上获取 POST 数据
【发布时间】:2018-01-07 02:53:42
【问题描述】:

我有一个在 c# 中运行的 Web API 服务器。 httpget 方法完美运行,但我对 Web Api 太陌生,无法让帖子正常工作,而且我所做的搜索有点无结果。

这是我在 ApiController 中的 HttpPost

    [HttpPost]
    public bool UploadLogs(UploadLogsIn logs)
    {
        return true;
    }

这是模型

public class UploadLogsIn
{
    public byte[] logData { get; set; }
    public int aLogs { get; set; }
}

在 C++ 应用程序中,我尝试将数据发布到此方法。我正在使用 Curl 来发帖

    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if (curl)
    {
        curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.56.109:9615/api/WebApiService/UploadLogs");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, R"({"UploadLogsIn": [{"aLogs: 10}]})");
        curl_easy_setopt(curl, CURLOPT_POST, 1L);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20);
        curl_easy_perform(curl);
    }

    curl_easy_cleanup(curl);

当我调试 Web Api 时,该方法被命中,但参数不包含任何数据。

更新 使用wireshark,这是发送的信息

POST /api/WebApiService/UploadLogs HTTP/1.1
Host: 192.168.56.109:9615
Accept: */*
Content-Length: 32
Content-Type: application/x-www-form-urlencoded

Form item: "{"UploadLogsIn": [{"aLogs: 10}]}" = ""

结束更新

如果我添加标题

        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Accept: application/json");
        headers = curl_slist_append(headers, "Content-Type: application/json");
        headers = curl_slist_append(headers, "charsets: utf-8");

那么我的参数为空。

为此的 Wireshark 转储是

POST /api/WebApiService/UploadLogs HTTP/1.1
Host: 192.168.56.109:9615
Accept: application/json
Content-Type: application/json
charsets: utf-8
Content-Length: 32

Line-based text data: application/json
{"UploadLogsIn": [{"aLogs: 10}]}

我确定我做错了一些愚蠢的事情。任何帮助将不胜感激

【问题讨论】:

  • 发送的数据不是格式良好的 JSON 数据。查看您是如何构建 JSON 对象的。类与发送的 json 不匹配
  • 我已将字符串更改为 `curl_easy_setopt(curl, CURLOPT_POSTFIELDS, R"({"UploadLogsIn": [{"aLogs": 10}]})"); JSON中还有什么问题吗?在这个阶段结果保持不变

标签: c# curl asp.net-web-api


【解决方案1】:

您想要的类与正在发送的数据不匹配。

你想要的类看起来像这样

public class UploadLogsIn {
    public byte[] logData { get; set; }
    public int aLogs { get; set; }
}

但是发送的数据是这样的

{"UploadLogsIn": [{"aLogs": 10}]}

反序列化后的样子

public class UploadLogsIn
{
    public int aLogs { get; set; }
}

public class RootObject
{
    public IList<UploadLogsIn> UploadLogsIn { get; set; }
}

看到区别了吗?

让模型绑定到这个动作

[HttpPost]
public bool UploadLogs([FromBody]UploadLogsIn logs) {
    return true;
}

发送的数据必须如下所示

{"aLogs": 10}

【讨论】:

  • 我对 null 的实际问题是因为我在 byte[] 中使用了无效信息。现在一切正常。
【解决方案2】:

我还在输入数据中添加了 [FromBody]。

我查看了 swagger 在 post 声明中建议我发送数据的内容:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{     "EmailAddress": "string",    "Title": "string",     Message": "string",     "OriginId": 0,     "Name": "string"   }'  http://wfadpqa.sidmar.be/SecureIt.Service/api/contact'

【讨论】:

    【解决方案3】:

    尝试在UploadLogs中使用[FromBody]

       [HttpPost]
        public bool UploadLogs([FromBody]UploadLogsIn logs)
          {
            return true;
          }
    

    【讨论】:

    • 对不起,我没有提到这一点。我已经添加了这一点,并在[HttpPost] 下方添加了 `[ResponseType(typeof(UploadLogsIn))]`
    • 没有效果?还是同样的问题?尝试使用 fiddler 来嗅探请求正文中发送的内容
    • 我正在使用 Wireshark。我将编辑我的问题以添加它
    • 试试这个...而不是public bool UploadLogs([FromBody]UploadLogsIn logs) 试试public bool UploadLogs([FromBody]object logs)。然后使用调试器检查该对象。
    • @Vladimir 查看正在发送的 JSON。是否形成良好?从那里开始。
    猜你喜欢
    • 2013-07-23
    • 2018-09-01
    • 2017-08-31
    • 2012-10-24
    • 2019-01-28
    • 2020-08-17
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    相关资源
    最近更新 更多