Search code examples
c++libcurlpastebintrinitycore

Uploading to pastebin


Plain and simple: I'm generating a list of integers from a database and I need to upload it to Pastebin.com. Here is my code:

curl_global_init(CURL_GLOBAL_ALL);
if (CURL* curl = curl_easy_init()) {
    std::ostringstream postField;
    postField << "api_user_key=" << "&" << "api_option=paste" << "&" << "api_paste_private=1" << "&";
    postField << "api_paste_name=GuidSet" << "&" << "api_paste_expire_date=10M" << "&";
    postField << "api_paste_format=text" << "&" << "api_dev_key=XXXXXXXXXXXXXXXX" << "&" << "api_paste_code=";
    for (std::set<uint32>::const_iterator itr = resultSet.begin(); itr != resultSet.end(); ++itr) {
        postField << *itr;
        if (itr != --resultSet.end())
            postField << ",";
    }

    struct MemoryStruct chunk;
    chunk.memory = (char*)malloc(1);
    chunk.size   = 1;

    curl_easy_setopt(curl, CURLOPT_URL, "http://pastebin.com/api/api_post.php");
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    char* postStr = curl_easy_escape(curl, postField.str().c_str(), postField.str().length());
    printf("POST parameters: %s\n", postStr);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postStr);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "TrinityCore GuidBot/1.0");

    curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    self->_parentThread.sendCommand("PRIVMSG %s :%s: Finished, data available at %s",
        searchParams.targetRoom.c_str(), searchParams.actionInvoker.c_str(), chunk.memory);

    if (chunk.memory)
        free(chunk.memory);
    curl_free(postStr);
}
curl_global_cleanup();

However, when executing that code, i always get this:

* About to connect() to pastebin.com port 80 (#0)
*   Trying 72.20.36.113...
* connected
* Connected to pastebin.com (72.20.36.113) port 80 (#0)
> POST /api/api_post.php HTTP/1.1
User-Agent: TrinityCore GuidBot/1.0
Host: pastebin.com
Accept: */*
Content-Length: 609
Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 609 out of 609 bytes
< HTTP/1.1 200 OK
< Server: nginx
< Date: Sun, 28 Oct 2012 11:21:33 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
<
* Connection #0 to host pastebin.com left intact
* Closing connection #0
Bad API request, invalid api_option

Is there anything I'm obviously doing wrong ? The chunk that should be pasted has to look like "48,50,59,...etc"


Solution

  • Solved it myself, the problem wasnt lying in CURL, but in streaming parameters, although printf'ing stuff didnt show up any space being inserted.