I want to create a cURL request in some C++ code which will get me the length of a file in a server without downloading the file. For that, I use some cURL options to tell I
Have you tried with CURLINFO_CONTENT_LENGTH_DOWNLOAD instead?
need call perform()
curl_easy_setopt(_curl_handle, CURLOPT_HEADER, 1); curl_easy_setopt(_curl_handle, CURLOPT_NOBODY, 1);
curl_easy_perform(_curl_handle);
curl_easy_getinfo(_curl_handle, CURLINFO_CONTENT_LENGTH_UPLOAD, &dResult);
CURLINFO_CONTENT_LENGTH_UPLOAD
is the number of bytes uploaded. You need to use CURLINFO_CONTENT_LENGTH_DOWNLOAD
instead.
Note that if the server dynamically generates the data, the length may be different when you actualy download the file versus just downloading its headers.
Also note that if the server sends data as compressed when downloaded, there may not be any size available in the headers (if the Transfer-Encoding
header is used instead of the Content-Length
header), so CURLINFO_CONTENT_LENGTH_DOWNLOAD
would still return -1
. The only way to know the size in that situation would be to download it in full.