(C++) libCURL POST get response back issue

Started by
3 comments, last by md_lasalle 12 years, 5 months ago
Hello, this might seem obvious to some of you that has experience with libcurl, but for some reason I'm fiddling with this and can't put my finger on the problem.

This is the code that sends the POST to a php page, it works 100%, as on the server I insert a record in a database when I receive the data, and all is fine.



CURL* easyhandle = curl_easy_init();
if( easyhandle )
{
m_recBuf = new char[1024];
curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(easyhandle, CURLOPT_URL, m_url.c_str());
curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, &WebQuery::WriteData);
curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, m_recBuf);
}
res = curl_easy_perform(easyhandle); /* post away! */

curl_easy_perform returns CURLE_GOT_NOTHING(52) , no header or data from the server

My issue now is that I need to find a way to get some response back from the call, I have declared a static function :



size_t WebQuery::WriteData( char *ptr, size_t size, size_t nmemb, void *userdata)
{
return size;
}


Even with a breakpoint, "return size;" never gets hit.


On the server, in the php script, I echo this :

echo "OK";

Is this the way a response should be sent ?
Why is my WriteData function never called ?

Thanks everyone.
This post is actually a cross post from Link , thought I'd get a better response in this sub forum.
Advertisement

This is the code that sends the POST to a php page, it works 100%, as on the server I insert a record in a database when I receive the data, and all is fine.
echo "OK";


It's quite likely that your problem is on the PHP side. Headers that are often required in responses include Content-Type and Content-Length. I don't know if PHP generates that for you correctly. You might want to use Wireshark or tcpdump to capture the data and see what's going across the wire.
Also, check the PHP error log for any clues.
enum Bool { True, False, FileNotFound };
Thanks for the tips, so my first thing was to get a tool like Firebug to observe the response headers coming from the server. Obviously, I had to trigger this from a form in another php file using the POST method so that Firebug could actually catch it.

This is what I have :


Response Headers

Date Thu, 08 Dec 2011 02:02:21 GMT
Server Apache
X-Powered-By PHP/5.2.17
Keep-Alive timeout=5, max=100
Connection Keep-Alive
Transfer-Encoding chunked
Content-Type text/html

Request Headers
Host www.mydomain.com
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:8.0.1) Gecko/20100101 Firefox/8.0.1
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection keep-alive
Referer http://www.mydomain.com/folder/submitTest.php


From my perspective, everything looks good, but I'm not a web expert.

I tried using the header() function in PHP as well, without any different result :

header('HTTP/1.1 200 OK');
header('Content-Type: text/html; charset=utf-8');
header('Content-Length: 3495');



I just ran it with VERBOSE and some STDERR redirected to a file and this is what i have :



* About to connect() to www.mydomain.com port 80 (#0)
* Trying 72.xx.xx.xx... * connected
* Connected to www.mydomain.com (72.xx.xx.xx) port 80 (#0)
> POST /folder/submitScore.php HTTP/1.1

Host: www.mydomain.com

Accept: */*

Content-Length: 50

Content-Type: application/x-www-form-urlencoded



* Empty reply from server
* Connection #0 to host www.mydomain.com left intact
* server returned nothing (no headers, no data)
* Closing connection #0

Ok looking at this post : http://stackoverflow.com/questions/5929971/c-curl-empty-reply-from-server-every-time it seems to be a bug with libcurl 7.16.2, which is the exact version I'm using, gonna move to latest now.
It was indeed a bug in the libcurl version, FYI for everyone, do not use 7.16.2!

Thanks hplus for your input.

This topic is closed to new replies.

Advertisement