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.

Gautham
  • 766
  • 7
  • 15
Gioia
  • 81
  • 1
  • 9

1 Answers1

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.

deltheil
  • 15,496
  • 2
  • 44
  • 64
  • 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? – Gioia Jun 04 '13 at 20:37
  • 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](https://github.com/Moodstocks/moodstocks-api/blob/master/moodstocks-api/msapi.c)). 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. – deltheil Jun 05 '13 at 08:47
  • 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. – Gioia Jun 05 '13 at 12:53
  • 1
    @Gioia it seems you found this answer useful, if so you should consider [accepting it](http://meta.stackexchange.com/questions/23138/how-to-accept-the-answer-on-stack-overflow). – Shafik Yaghmour Nov 14 '13 at 21:05