cURL / Mailing Lists / curl-library / Single Mail

curl-library

PUT data (not file) - can't make it work

From: Tiberiu Motoc <tiberiu.motoc_at_gmail.com>
Date: Tue, 11 Mar 2008 21:24:16 -0700

Hi everyone,

I'm trying to perform a PUT and send some string data. There are examples
out there which show PUT with a file, or POST with string data, but I can't
find anything for PUT with string data. I've been struggling with this for
about 8 hours and just cannot get to the bottom of it.

From the command line, you would execute something like this:
curl -X PUT -d "user[age]=26" http://localhost:3000/users/30 -v

This is my code:
...
int counter = 0;
curl_easy_setopt( internetSession, CURLOPT_URL, url);
curl_easy_setopt( internetSession, CURLOPT_VERBOSE, 1);
curl_easy_setopt( internetSession, CURLOPT_HEADER, 1);
curl_easy_setopt( internetSession, CURLOPT_READFUNCTION, put_callback);
curl_easy_setopt( internetSession, CURLOPT_READDATA, &counter);
curl_easy_setopt( internetSession, CURLOPT_UPLOAD, 1);
curl_easy_setopt( internetSession, CURLOPT_PUT, 1);
curl_easy_setopt( internetSession, CURLOPT_INFILESIZE, strlen(c_nv_pairs));
curl_easy_perform(internetSession);
...
size_t CInternetRes::put_callback(void* buffer, size_t size, size_t nmemb,
void* userp) {
int *counter = (int *)userp;
char* message = "user[age]=26";
if (*counter) {
cout<<"READ ALREADY DONE"<<endl;
return 0;
}
(*counter)++;
if (size * nmemb > strlen((char*)message)) {
cout<<"READ"<<endl;
memcpy(buffer, (void*) message, strlen((char*)message));
return strlen((char*)message);
}
cout<<"READ NOT FINE"<<endl;
return 0;
}

The output shows the following (last 2 lines are my messages):
* About to connect() to localhost port 3001 (#0)
* Trying 127.0.0.1... * connected
* Connected to localhost (127.0.0.1) port 3001 (#0)
> PUT /users/30.xml HTTP/1.1
Host: localhost:3001
Accept: */*
Content-Length: 15
Expect: 100-continue
READ
READ ALREADY DONE

My web server is acting very strange: it shows a RequestTimeout and then it
reports an ERROR. I've never seen this kind of error before.

The output of the curl command line (mentioned above) is:
* About to connect() to localhost port 3001 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 3001 (#0)
> PUT /users/30 HTTP/1.1
> User-Agent: curl/7.16.4 (i486-pc-linux-gnu) libcurl/7.16.4 OpenSSL/0.9.8e
zlib/1.2.3.3 libidn/1.0
> Host: localhost:3001
> Accept: */*
> Content-Length: 13
> Content-Type: application/x-www-form-urlencoded

I did add the content-type in the header, but that made no difference. If I
remove the INFILESIZE option (which I thought I should since I'm not sending
in a file), then the web server doesn't error out, however, it doesn't
receive the string data.

I would appreciate any help.

Thanks,
Tiberiu
Received on 2008-03-12