Using Digest Auth and Sending POST data with curl
-
08-07-2021 - |
Question
I have a question regarding HTTP Digest Auth using curl, and sending post data at the same time.
I have a REST API, and each resource in it needs authentication. I access my resources from my website using php curl. Each resource needs to authenticate using digest.
I'm using CURLAUTH_DIGEST for this, and it usually works like a charm.
The problem is when I try to send additional data in POST, using CURLOPT_POSTFIELDS, that data will not be sent alongside with digest auth information. Why is this? How can I fix this?
I'm using PHP 5.4.3 with php curl 7.25.0
Solution
There is absolutely no problem to combine Digest authentication with POST
requests.
Make sure to double check if you are suppose to do:
- a basic
POST
(the equivalent of-X POST -d 'body-content-here'
withcurl
CLI), - or a
multipart POST
(e.g.-F image_file=@"foo.jpg"
to upload a given file).
Also what I greatly recommend is to interact with your API via curl
command-line tool and use the --libcurl request.c
: it will write into this output C file a convenient recap of all the options used to perform the request, and then you will simply have to copy/adapt them on your side.
At last, feel free to refer to this PHP code sample that illustrates how to perform such calls.