Responses
Every HTTP request includes an HTTP response. An HTTP response is a set ofmetadata and a response body, where the body can occasionally be zero bytesand thus nonexistent. An HTTP response however always has response headers.
Response body
The response body is passed to thewrite callbackand the response headers to theheader callback.
Virtually all libcurl-using applications need to set at least one of thosecallbacks instructing libcurl what to do with received headers and data.
Response meta-data
libcurl offers thecurl_easy_getinfo()
function that allows an applicationto query libcurl for information from the previously performed transfer.
Sometimes an application just want to know the size of the data. The size ofa responseas told by the server headers can be extracted withcurl_easy_getinfo()
like this:
curl_off_t size;curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &size);
If you can wait until after the transfer is already done, which also is a morereliable way since not all URLs provide the size up front (like for examplefor servers that generate content on demand) you can instead ask for theamount of downloaded data in the most recent transfer.
curl_off_t size;curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD_T, &size);
HTTP response code
Every HTTP response starts off with a single line that contains the HTTPresponse code. It is a three digit number that contains the server's idea ofthe status for the request. The numbers are detailed in the HTTP standardspecifications but they are divided into ranges that work like this:
Code | Meaning |
---|---|
1xx | Transient code, a new one follows |
2xx | Things are OK |
3xx | The content is somewhere else |
4xx | Failed because of a client problem |
5xx | Failed because of a server problem |
You can extract the response code after a transfer like this
long code;curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
About HTTP response code "errors"
While the response code numbers can include numbers (in the 4xx and 5xxranges) which the server uses to signal that there was an error processing therequest, it is important to realize that this does not make libcurl return anerror.
When libcurl is asked to perform an HTTP transfer it returns an error if thatHTTP transfer fails. However, getting an HTTP 404 or the like back is not aproblem for libcurl. It is not an HTTP transfer error. A user might be writinga client for testing a server's HTTP responses.
If you insist on curl treating HTTP response codes from 400 and up as errors,libcurl offers theCURLOPT_FAILONERROR
option that if set instructs curl toreturnCURLE_HTTP_RETURNED_ERROR
in this case. It then returns error as soonas possible and does not deliver the response body.