curl-users
Having trouble getting http put to work using libcurl
Date: Fri, 11 Apr 2003 12:33:15 +0200
Hi,
I'm having touble getting http put to work using the libcurl.dll (Windows).
The file is created, but has zero size. Uploading works fine however if I
use the curl command-line tool. I get the same results if I use the static
library.
Here is an excerpt of my implementation (which reproduces the problem), any
help is appreciated!
Best Regards,
David
<clip>
// CurlTest2.cpp : Defines the entry point for the console application.
//
#include "StdAfx.h"
#include "curl/curl.h"
#include <iostream>
#include <strstream>
#include <assert.h>
extern "C" {
size_t readData(char *ptr, size_t size, size_t nmemb, void *userp);
}
size_t readData(char *ptr, size_t size, size_t nmemb, void *userp)
{
if (0 == ptr || 0 == userp) {
assert(0);
return 0;
}
assert(size*nmemb != 0);
std::istream* pIs = reinterpret_cast<std::istream*>(userp);
int nLen = 0;
char pszBuf[CURL_MAX_WRITE_SIZE];
pIs->read(pszBuf, CURL_MAX_WRITE_SIZE);
nLen = pIs->gcount();
memcpy(ptr, pszBuf, nLen);
return nLen;
}
BOOL SendStreamToFile(std::istream& is, char* pszUri)
{
CURLcode code = curl_global_init(CURL_GLOBAL_ALL);
assert(code == CURLE_OK);
CURL* pCurl = curl_easy_init();
if (0 == pCurl)
return FALSE;
curl_easy_setopt(pCurl, CURLOPT_VERBOSE, TRUE);
curl_easy_setopt(pCurl, CURLOPT_READFUNCTION, readData);
curl_easy_setopt(pCurl, CURLOPT_UPLOAD, TRUE) ;
curl_easy_setopt(pCurl, CURLOPT_PUT, TRUE);
curl_easy_setopt(pCurl, CURLOPT_URL, pszUri);
curl_easy_setopt(pCurl, CURLOPT_INFILE, &is);
// If I set the size then the file is created correctly
// but I still do not get CURLE_OK.
// curl_easy_setopt(pCurl, CURLOPT_INFILESIZE, 14);
//
CURLcode result = curl_easy_perform(pCurl); // Should do something with
return value.
curl_easy_cleanup(pCurl);
return result == CURLE_OK ? TRUE : FALSE;
}
int main(int argc, char** argv)
{
std::strstream str;
str << "Hello, world!" << std::endl;
char* pszUri = "http://localhost/test.out <http://localhost/test.out> ";
if (SendStreamToFile(str, pszUri)) {
std::cout << "Success" << std::endl;
return 0;
}
else {
std::cout << "Failure" << std::endl;
return 1;
}
}
</clip>
----------------------------------------------------------------------------
---------------------------------------------------------------------
Tau Generation2 - Development Visualized. Productivity Relalized.
Learn more about Telelogic's new system and software development solution
at: http://www.taug2.com <http://www.taug2.com> .
----------------------------------------------------------------------------
-----------------------------------------------------------------------
-------------------------------------------------------
This SF.net email is sponsored by: Etnus, makers of TotalView, The debugger
for complex code. Debugging C/C++ programs can leave you feeling lost and
disoriented. TotalView can help you find your way. Available on major UNIX
and Linux platforms. Try it free. www.etnus.com
Received on 2003-04-11