Using libcurl to send protocol buffer messages over HTTP

1,236 views
Skip to first unread message

samarules

unread,
Jan 17, 2010, 6:10:01 AM1/17/10
to Protocol Buffers
Hi,

I am trying to send protocol buffer messages over HTTP using libcurl
in C++. I am POSTing in binary mode. It seems, upon posting data gets
corrupt.

Here is the code snippet:

-------------------------------
std::string st;
campaignpool.SerializeToString(&st);

//POST using curl

struct curl_slist *headers=NULL;
headers = curl_slist_append(headers, "Content-Type: application/octet-
stream");

/* post binary data */
easyhandle = curl_easy_init();
curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDS, &st);
curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDSIZE, st.size());
curl_easy_setopt(easyhandle, CURLOPT_HTTPHEADER, headers);
.........
.........
curl_easy_perform(easyhandle); /* post away! */

----------------------------------

Any help would be highly appreciated.


Regards,
Zia

Evan Jones

unread,
Jan 17, 2010, 10:37:24 AM1/17/10
to samarules, Protocol Buffers
On Jan 17, 2010, at 6:10 , samarules wrote:
> curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDS, &st);

st is a std::string. CURLOPT_POSTFIELDS need a pointer to the data
itself. See:

http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTPOSTFIELDS


Use:

curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDS, st.data());


Evan

--
Evan Jones
http://evanjones.ca/

Reply all
Reply to author
Forward
0 new messages