Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading