how to use cURL with TLS implementation

Hello

I'm evaluating cURL with nrf9160DK,zephyrOS.

Finally, it goes well with cURL - http - however, it cant do with TLS - https -

Could you let me know how to implement TLS/SSL for using cURL ?

Does it require some config ?

Here is my snipet code. (non - TLS)

-------------------------------------------------------------

static size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
char **response_ptr = (char **)userp; // Cast userp to the correct type
char *response = realloc(*response_ptr, strlen(*response_ptr) + realsize + 1);

if (response == NULL) {
LOG_ERR("Not enough memory to store response\n");
return 0; // If realloc fails, return 0 to stop curl
}

*response_ptr = response;
strncat(response, (char *)contents, realsize);

return realsize;
}

static void http_request_example(void) {
CURL *hnd;
CURLcode ret;
char *response = malloc(4096); // Allocate initial memory for the response
if (response == NULL) {
LOG_ERR("Memory allocation failed for response buffer");
return;
}
response[0] = '\0'; // Initialize the response buffer

LOG_INF("Starting HTTP request example");

// Initialize libcurl
hnd = curl_easy_init();
if (hnd) {
// Set the URL for the HTTP GET request
curl_easy_setopt(hnd, CURLOPT_URL, "">http://httpbin.org/get");

// Set the callback function for handling response data
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, (void *)&response);

#if 1
curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
if (curl_info->protocols) {
const char * const *proto;
for (proto = curl_info->protocols; *proto; ++proto) {
LOG_INF("Supported protocol: %s\n", *proto);
}
}
#endif

// Perform the request
ret = curl_easy_perform(hnd);

// Check if the request was successful
if (ret != CURLE_OK) {
LOG_ERR("curl_easy_perform() failed: %s", curl_easy_strerror(ret));
} else {
// Print the response
LOG_INF("Response: %s", response);
}

// Clean up and free the resources
curl_easy_cleanup(hnd);
free(response);
} else {
LOG_ERR("Failed to initialize curl");
}
}

Related