我有一個運行在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