我有一个运行在c#中的Web API服务器。 httpget方法可以完美工作,但对于Web Api来说,我太过于陌生,无法让帖子起作用,而且我所做的搜索功能有些徒劳无功。在Web API上获取POST数据
这是我HttpPost在ApiController
[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时,该方法被击中,但该参数不包含任何数据。
UPDATE 使用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");
那么我的参数为null 。
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不匹配 – Nkosi
我已将字符串更改为'curl_easy_setopt(curl,CURLOPT_POSTFIELDS,R“({”UploadLogsIn“:[{”aLogs“:10}]})”); JSON中还存在什么错误?结果在这个阶段保持不变 – Jaques