MIke
MIke

Reputation: 17

libcurl: http response status code always NULL?

I am using libcurl 7.29.0.

All my test cases are working fine when the server is running, but when the server is not reachable I'm receiving a CURLE_HTTP_RETURNED_ERROR error.

In this scenario I'm always getting a CURLE_COULDNT_CONNECT curl error and the HTTP response code is 0.

Based on my requirement I want the HTTP response code. Please suggest some solution thank in advance.

Below is attached the API I'm using to post data.

int http_curl_post_response(char *url, char *post_data, char *response,
                            long int *responseCode) {
    CURL *ps_curl;
    CURLcode curl_status = CURLE_OK;
    struct curl_slist *headers = NULL; /* http headers to send with request */

    /* get a curl handle */
    ps_curl = curl_easy_init();
    if (ps_curl) {
        /* set content type */
        headers = curl_slist_append(headers, "Accept: application/json");
        headers = curl_slist_append(headers, "Content-Type: application/json");
        headers = curl_slist_append(headers, "charsets: utf-8");

        /* This is to intialize respone from curl */
        http_curl_respone ps_response;

        curl_easy_setopt(ps_curl, CURLOPT_URL, url);

        curl_easy_setopt(ps_curl, CURLOPT_HTTPHEADER, headers);

        curl_easy_setopt(ps_curl, CURLOPT_POSTFIELDS, post_data);

        curl_easy_setopt(ps_curl, CURLOPT_POSTFIELDSIZE, -1L);

        curl_easy_setopt(ps_curl, CURLOPT_FOLLOWLOCATION, 1L);

        /* This is to read response from curl */
        curl_easy_setopt(ps_curl, CURLOPT_WRITEFUNCTION, write_response_json);

        init_response(&ps_response);
        if (ps_response.pi1_response == NULL) {
            i4_curl_status = MEM_ALLOC_ERROR;
            goto HTTP_CURL_ERROR;
        }

        curl_easy_setopt(ps_curl, CURLOPT_WRITEDATA, &ps_response);

        curl_easy_setopt(ps_curl, CURLOPT_FAILONERROR, 1L);

        /* Perform the request, i4_curl_status will get the return code */
        curl_status = curl_easy_perform(ps_curl);
        if (curl_status != CURLE_OK) {
            if (curl_status == CURLE_HTTP_RETURNED_ERROR) {
                curl_easy_getinfo(ps_curl, CURLINFO_RESPONSE_CODE,
                                  responseCode);
                /* always cleanup */
                curl_easy_cleanup(ps_curl);
            }
            goto HTTP_CURL_ERROR;
        } else {
            memcpy(response, ps_response.pi1_response, ps_response.s_len);
            response[ps_response.s_len] = '\0';
        }

        /* always cleanup */
        curl_easy_cleanup(ps_curl);
        /* free headers */
        curl_slist_free_all(headers);

    HTTP_MEM_ERROR:
        if (ps_response.pi1_response) free(ps_response.pi1_response);
    } else {
        curl_status = CURLE_FAILED_INIT;
    }

HTTP_CURL_ERROR:

    return curl_status;
}

Upvotes: 1

Views: 2263

Answers (1)

David Collins
David Collins

Reputation: 3022

... but when the server is not reachable I'm receiving a CURLE_HTTP_RETURNED_ERROR error. In this scenario I'm always getting a CURLE_COULDNT_CONNECT curl error and the HTTP response code is 0.

It doesn't even make sense to examine the response code if the server is not reachable and the connection failed. The response code is a property of the HTTP response. It is recorded in part of the HTTP response (the status line) that the HTTP server sends back in reply to the HTTP request that libcurl sends on your behalf. If there is no connection, the server did not receive your request and there is no response to speak of!

Based on my requirement I want the HTTP response code.

If there is no connection, there is no response - and hence no response code.

In this scenario, you could set your local responseCode variable to -1 to indicate connection failure:

*responseCode = -1;

Upvotes: 3

Related Questions