[英]I cant send a message with a discord webhook using cURL error : "Cannot send an empty message
因此,我嘗試使用以下代碼向不和諧的 webhook 發送消息:
#include <iostream>
#include <curl/curl.h>
int main(void)
{
CURL* curl;
CURLcode res;
const char* WEBHOOK = "webhookLink";
const char* content = "test";
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, WEBHOOK);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}
我從 cURL 文檔中得到了這段代碼。 每次我運行它時,它都會在控制台中輸出{"message": "Cannot send an empty message", "code": 50006} 。
有任何想法嗎?
編輯:它與命令行一起使用
curl -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"content\": \"Posted Via Command line\"}" $WEBHOOK_URL
您需要將Content-Type
標頭添加到您的請求中。
示例(我沒有不和諧的 webhook,所以我無法測試它):
#include <curl/curl.h>
#include <iostream>
int main(void) {
CURL* curl;
CURLcode res;
const char* WEBHOOK = "webhookLink";
const char* content = R"aw({"content": "Posted Via libcurl"})aw";
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, WEBHOOK);
// create a curl list of header rows:
struct curl_slist* list = NULL;
// add Content-Type to the list:
list = curl_slist_append(list, "Content-Type: application/json");
// set this list as HTTP headers:
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content);
res = curl_easy_perform(curl);
curl_slist_free_all(list); // and finally free the list
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}
(還應添加額外的錯誤檢查)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.