curl with https example

good example code in c linux using libcurl with https, curl over https
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nociSoftware EngineerCommented:
I have no code myself.. but have you looked at the source of the commandline tool of curl.

All things you need from the commandline are translated to libcurl commands.
The following options are relevant to ssl:
--engine, --egd-file, --cert, --cert-type, --cacert, --capath, --insecure, --key, --key-type, --random-file, -1, -2, -3
tittuAuthor Commented:
please provide any example code using https and curl
nociSoftware EngineerCommented:
Here is the source:

Look at the curl commandline tool for the handling of the above items.

At least --cacert / --capath is needed for verifying the remote server.
--cert / --key might be needed if you need to authenticate

The others depend on specific use -2 = ssl2, -3 = ssl3, -1 = tls
if you use special ssl hanling (engines) you need to select one... etc.
But the use of that SHOULD be documented with assignment you have.
Redefining Cyber Security w/ AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Join our webinar on Sept. 21st to learn more about leveraging AI and machine learning to protect your business.

HonorGodSoftware EngineerCommented:

See sections
11.1 Certificates

Does this help?
tittuAuthor Commented:
I am asking curl api to send/get https request and the api usage example , not the curl source code
nociSoftware EngineerCommented:
The curl source code DOES use the api. The commandline tool is a very thin layer where a commandline option
like --capath is just calling everything that is needed to specify a CAPATH to the curl layer.
So if you actually care to check the curl commandline source, lookup where --capath is handled you know what to do.
You can prototype on a command line the exact options you need and spell those out.

(And the curl code is quite good accessible, I did the initial port of it to OpenVMS some years ago.)
I think this is what you would want (it is in C++).
#include <stdio.h>
#include <curl/curl.h>
int main(void)
  CURL *curl;
  CURLcode res;
  curl = curl_easy_init();

  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "");
    res = curl_easy_perform(curl);
    /* always cleanup */ 

  return 0;

Open in new window

nociSoftware EngineerCommented:
Yes and some options for setting up certificate checking. like specifying the CA_PATH etc.
and you example is still C.
tittuAuthor Commented:
C/C++ no problem how to setup curl with certificate .pem file
nociSoftware EngineerCommented:

is a good starting point:
CURLOPT_CAPATH  (to verify remote)
CURLOPT_SSL_VERIFYPEER (if you want to verify)

CURLOPT_CERT & CURLOPT_SSLKEY etc. are used to specify an authentication cert.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tittuAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.