3

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 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried, but it does not work properly...I should receive the challenge and I don't receive anything, do I have to write more code after the code you send me? do I have to set headers or something similar?
Then you should have a problem server-side. Of course you must set the CURLOPT_URL option, but otherwise this is all that you need. And I do confirm it works (e.g see these real world examples). Once again I do recommend you to perform the same request on the command-line, on your own service, with the --libcurl out.c option: it will output everything you need.
Yeah! now it works! It was a problem of the server as you suggest. I thank you so much my friend, have a good day.
Hi, what was the problem on server side. I faced the same situation while downloading a file.
@Gioia it seems you found this answer useful, if so you should consider accepting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.