3

I'm sending an incorrect URL to a POST request via libcurl in C code. The server sends back a 400 along with a response containing detailed information about why the request was rejected. in libcurl i can see the error code:

res CURLcode    CURLE_HTTP_RETURNED_ERROR

i can also see protocol error via:

char errbuf[CURL_ERROR_SIZE];
errbuf[0] = 0;
curl_easy_setopt(vc->curl, CURLOPT_ERRORBUFFER, errbuf);
/* Perform the request, resCode will get the return code */
CURLcode resCode = curl_easy_perform(vc->curl);


errbuf  char [256]  "The requested URL returned error: 400 Bad Request" 

what i would really like to access is the body of the response. the server puts detailed error information there about why it rejected the request:

[
    {
        "code": "io.myorg.bad.request",
        "message": "Unrecognized query parameter 'count' with value: true.",
        "params": []
    }
]

i have read and write callback functions set via:

/* ask curl to let us know if we get a 400 or higher */
curl_easy_setopt(vc->curl, CURLOPT_FAILONERROR, 1L);

curl_easy_setopt(vc->curl, CURLOPT_HTTPHEADER, vc->slist);

/* we want to use our own read/write function */
curl_easy_setopt(vc->curl, CURLOPT_READFUNCTION, read_callback);
curl_easy_setopt(vc->curl, CURLOPT_WRITEFUNCTION, write_callback);

/* pointer to pass to our callback functions */
curl_easy_setopt(vc->curl, CURLOPT_READDATA, vc);
curl_easy_setopt(vc->curl, CURLOPT_WRITEDATA, vc); 

they are not called in this case. how do i get at the json in the response message?

jmer
  • 371
  • 2
  • 12

1 Answers1

2

Enabling the CURLOPT_FAILONERROR option prevents the write callback from being called when an error is detected.

Andrew Lambert
  • 1,869
  • 1
  • 17
  • 31