因此,我试图使用以下代码向一个不和谐的网络钩子发送消息:
#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“:”无法发送空消息“、"code":50006}。
有什么想法吗?
编辑:它与命令行一起工作。
curl -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"content\": \"Posted Via Command line\"}" $WEBHOOK_URL发布于 2022-07-05 20:38:55
您需要将Content-Type头添加到请求中。
示例(我没有不和谐的网络钩子,所以我无法测试它):
#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();
}(还应添加其他错误检查)
https://stackoverflow.com/questions/72874966
复制相似问题