본문 바로가기
BackEnd FrontEnd/C , C++

c++ : send wav data with curl

by forkballpitch 2019. 10. 3.
반응형
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <fstream>


static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

int main(void)
{

    std::string contents;
    std::ifstream in("test2.wav", std::ios::in | std::ios::binary);

    if (in)
    {
        in.seekg(0, std::ios::end);
        contents.resize(in.tellg());
        in.seekg(0, std::ios::beg);
        in.read(&contents[0], contents.size());
        in.close();
    }
    


    CURL *curl;
    CURLcode res;

    struct curl_httppost *formpost = NULL;
    struct curl_httppost *lastptr = NULL;
    struct curl_slist *headerlist = NULL;
    static const char buf[] =  "Expect:";

    curl_global_init(CURL_GLOBAL_ALL);

    curl = curl_easy_init();

    headerlist = curl_slist_append(headerlist, buf);
    if (curl) {

        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: audio/wav");

        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
       
        curl_easy_setopt(curl, CURLOPT_POST,1);
        curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:9080");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, contents.length());
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, contents.data());

        res = curl_easy_perform(curl);
        /* Check for errors */
        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));
        curl_easy_cleanup(curl);
        curl_slist_free_all(headerlist);
    }
    return 0;
}
반응형