How to post http request using digest authenticati

2019-02-25 07:55发布

问题:

I'm trying to perform a post request and I'm trying to do it with the digest authentication. with libcurl, I set the options:

curl_easy_setopt(curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);

curl_easy_setopt(curl_handle, CURLOPT_USERPWD, "username:password");

before setting all the other option (post, url and everything). The server closes my connection and I think that no digest is made. I just don't know how to automatically obtain the challenge-response behaviour of the digest. If I set HTTPAUTH to CURLAUTH_BASIC it encodes the stuff, I see with the VERBOSE option the header containing authorization = basic. With digest no headers.

Do you know how can I do it, or can you give me some example? I really searched everywhere.

回答1:

For a basic POST request you should do:

curl_easy_setopt(hnd, CURLOPT_USERPWD, "user:pwd");
curl_easy_setopt(hnd, CURLOPT_HTTPAUTH, (long)CURLAUTH_DIGEST);
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");

For a multipart POST (a.k.a multipart/form-data):

struct curl_httppost *post;
struct curl_httppost *postend;
/* setup your POST body with `curl_formadd(&post, &postend, ...)` */
curl_easy_setopt(hnd, CURLOPT_USERPWD, "user:pwd");
curl_easy_setopt(hnd, CURLOPT_HTTPPOST, post);
curl_easy_setopt(hnd, CURLOPT_HTTPAUTH, (long)CURLAUTH_DIGEST);
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");

Pro-tip: use curl command-line tool with --libcurl request.c: it outputs into this C file the list of options used to perform the corresponding request.



标签: c libcurl digest