How should I edit my programm to provide succ essfull execution of my request to the privat e server

Владислав Коробов via curl-li brary <[email protected]>
Newsgroups gmane.comp.web.curl.library
Message-ID <[email protected]>
Hello everyone,
I work on Linux #astra31+ci49-Ubuntu with  GNU/Linux. 
On my system installed curl 7.68.0 (x86_64-pc-linux-gnu) libcurl/7.68.0 OpenSSL/1.1.1k zlib/1.2.11 brotli/1.0.7 libidn2/2.0.5 libpsl/0.20.2 (+libidn2/2.0.5) libssh2/1.11.0 nghttp2/1.36.0 librtmp/2.3.
From this system I can get the data from some device usung console request to the private https server via the curl command:
 
curl -k -X GET -H 'Authorization: Bearer nokey'   https://<IP-address>:<port>/api/ccu/online
 
This command requests the device-IDs, connected to this server
As a response to this command I got data in JSON-format, that represents real data from selected device:
 
["860621065932826"]
 
This number represents the device-ID of connected device.
I would like to create a programm on base of C-examples from your site, using the calls of the libcurl, which could perform the same task as the console curl command mentioned above.
 
This is the text of my C-programm, having the name curl_example.c:
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
#include <curl/curl.h>
#include <curl/easy.h>
 
static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE* data)
{
  int written = fwrite(ptr, size, nmemb, data);
 
  printf("write_data: was written %i bytes into the file %s\n", written, data);
 
  return written;
}
 
int main(void)
{
  CURL *curl_handle;
    
  // Open files for the header and the body
  static const char *headerfilename = "head.txt";
  static const char *bodyfilename = "body.html";
  CURLcode Ret;
 
  FILE *header_file = fopen(headerfilename, "w");
  if (header_file == NULL)
  {
    printf("Can't open %s header_file", headerfilename);
    return -1;
  }
  
  FILE *body_file = fopen(bodyfilename, "w");
  if (body_file == NULL)
  {
    printf("Can't open %s body_file", bodyfilename);
    return -1;
  }
 
  curl_global_init(CURL_GLOBAL_ALL);
 
  /* curl session ininializatio */ 
  curl_handle = curl_easy_init();
  if (!curl_handle)
  {
    printf("Error after curl_easy_init, curl_handle=%l\n", curl_handle);
    return -1;
  }
 
  /* setup URL */ 
  Ret = curl_easy_setopt(curl_handle, CURLOPT_URL, "https://172.16.30.13:8443/api/ccu/online");
  if (Ret != CURLE_OK)
  {
     printf("Error %i after curl_easy_setopt CURLOPT_URL\n", Ret);
        curl_easy_cleanup(curl_handle);
        return -1;
  }
     
  /* set authentification */
  Ret = curl_easy_setopt(curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
  if (Ret != CURLE_OK)
  {
    printf("Error %i after curl_easy_setopt CURLOPT_HTTPAUTH\n", Ret);
    curl_easy_cleanup(curl_handle);
    return -1;
  } 
 
  Ret = curl_easy_setopt(curl_handle, CURLOPT_XOAUTH2_BEARER, "nokey");
  if (Ret != CURLE_OK)
  {
    printf("Error %i after curl_easy_setopt CURLOPT_XOAUTH2_BEARER\n", Ret);
    curl_easy_cleanup(curl_handle);
    return -1;
  } 
 
  /* perform GET request */
  Ret = curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1L);
  if (Ret != CURLE_OK)
  {
    printf("Error %i after curl_easy_setopt CURLOPT_HTTPGET\n", Ret);
    curl_easy_cleanup(curl_handle);
    return -1;
  } 
  
  /* no progress meter please */
  Ret = curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
  if (Ret != CURLE_OK)
  {
      printf("Error %i after curl_easy_setopt CURLOPT_NOPROGRESS\n", Ret);
         curl_easy_cleanup(curl_handle);
        return -1;
  }
  
  /* declare write_data as callback */
  Ret = curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
  if (Ret != CURLE_OK)
  {
    printf("Error %i after curl_easy_setopt CURLOPT_WRITEFUNCTION\n", Ret);
    curl_easy_cleanup(curl_handle);
    return -1;
  }
 
  /* save the body  */
  Ret = curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, body_file);
  if (Ret != CURLE_OK)
  {
    printf("Error %i after curl_easy_setopt CURLOPT_WRITEDATA\n", Ret);
        curl_easy_cleanup(curl_handle);
        return -1;
  }
 
  // save the header 
  Ret = curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, header_file);
  if (Ret != CURLE_OK)
  {
    printf("Error %i after curl_easy_setopt CURLOPT_HEADERDATA\n", Ret);
        curl_easy_cleanup(curl_handle);
        return -1;
  }
  
  /* get it! */
  Ret = curl_easy_perform(curl_handle);
  if (Ret != CURLE_OK)
  {
    printf("Error %i after curl_easy_perform\n", Ret);
        curl_easy_cleanup(curl_handle);
        return -1;
  }
 
  /* close the header file */
  fclose(header_file);
 
  /* close the body file */
  fclose(body_file);
 
  /* cleanup curl stuff */
  curl_easy_cleanup(curl_handle);
 
  printf("\nDone!\n");
  getchar();
  return 0;
}
 
Excuse me, that I put here whole text, becase I really don’t know, where could be a mistake.
I compiled the programm with command:
 
gcc -o curl_example curl_example.c -lcurl
 
and then started it putting as command
./curl_example
 
End, finally I got error message:
 
Error 60 after curl_easy_perform
 
Here is an explanation:
 
  CURLE_PEER_FAILED_VERIFICATION, /* 60 - peer's certificate or fingerprint  wasn't verified fine */
 
I am a novice in programing in this area. What I did incorrectly and how it should be done? 
 
Thanks in advance!
--
Vladislav Korobov
--
 
 
 
 

-- 
Unsubscribe: https://lists.haxx.se/mailman/listinfo/curl-library
Etiquette:   https://curl.se/mail/etiquette.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.