Home > Mobile >  I cant send a message with a discord webhook using cURL error : "Cannot send an empty message
I cant send a message with a discord webhook using cURL error : "Cannot send an empty message

Time:07-06

So im trying to send a message to a discord webhook using this code:

#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();
}

I got this code from the cURL docs. Every time i run this it outputs {"message": "Cannot send an empty message", "code": 50006} in the console.

Any ideas?

Edit: it worked with the command line

curl -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"content\": \"Posted Via Command line\"}" $WEBHOOK_URL

CodePudding user response:

You need to add the Content-Type header to your request.

Example (I have no discord webhook so I can't test it):

#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();
}

(additional error checking should also be added)

  • Related