Discussion:
HTTP Post with json body and client SSL certificate validation
Victor Dodon
2013-09-27 11:28:29 UTC
Permalink
Hello all!

I am working on a server that needs to do some push notifications for
devices with Android, iOS and Window Phone and it's the first time I use
libcurl. I know that the api reference can be found at [1], but I will ask
a few questions that I have not found answers in the api reference.

Q1. I need to set some custom headers for each request. The list of custom
headers can be freed imediately after curl_easy_setopt(curl,
CURLOPT_HTTPHEADER, slist) or only after the curl_easy_perform has returned?

Q2. How to do a post request with json HTTP body? Is something like this:

char *post_body = json_dumps(json, 0);
struct curl_slist *slist=NULL;
slist = curl_slist_append(slist, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_body);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(post_body);

?

Q3. Microsoft and Apple server require client SSL certificate validation.
Assuming that I have a suitable SSL certififcate, what options I need to
set on curl handles to achieve this? CURLOPT_SSLCERT
and CURLOPT_SSLCERTTYPE are enough?

Thank you very much!
Victor Dodon.

[1] http://curl.haxx.se/libcurl/c/
Cédric Deltheil
2013-09-27 12:31:48 UTC
Permalink
Q1. I need to set some custom headers for each request. The list of custom headers can be freed imediately after curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist) or only after the curl_easy_perform has returned?
After. See the HTTP custom header example[1].
char *post_body = json_dumps(json, 0);
struct curl_slist *slist=NULL;
slist = curl_slist_append(slist, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_body);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(post_body);
?
Yes. You do not even need to pass the CURLOPT_POSTFIELDSIZE since by default libcurl will do the strlen() for you.

Also, I would say CURLOPT_POST is redundant: you can omit it.
Q3. Microsoft and Apple server require client SSL certificate validation. Assuming that I have a suitable SSL certififcate, what options I need to set on curl handles to achieve this? CURLOPT_SSLCERT and CURLOPT_SSLCERTTYPE are enough?
Don't know about that :)

[1]: http://curl.haxx.se/libcurl/c/httpcustomheader.html

--
Cédric Deltheil
http://about.me/deltheil
Daniel Stenberg
2013-09-27 21:19:31 UTC
Permalink
Post by Victor Dodon
Q3. Microsoft and Apple server require client SSL certificate validation.
Assuming that I have a suitable SSL certififcate, what options I need to set
on curl handles to achieve this? CURLOPT_SSLCERT and CURLOPT_SSLCERTTYPE are
enough?
Client certificates are also used in combination with a private key so you
normally also need CURLOPT_SSLKEY, CURLOPT_KEYPASSWD and possibly
CURLOPT_SSLKEYTYPE.
--
/ daniel.haxx.se
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Loading...