mercredi 5 mai 2021

Nested libcurl request c++

My question would be how would I do a nested curl request because my current code is this and does not work. I does not work as I can see the requests going to my server and there is only one which is a GET request.

#include <curl/curl.h>
#include <iostream>

int rad = rand() % 100000;
std::string agent_name = "https://127.0.0.1:443/agent/" + std::to_string(rad);


int main(int argc, char *argv[])
{
  CURLcode res;
  CURL *curl;

  curl = curl_easy_init();
  curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 102400L);
  curl_easy_setopt(curl, CURLOPT_URL, "https://127.0.0.1:443/");
  curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  curl_easy_setopt(curl, CURLOPT_FTP_SKIP_PASV_IP, 1L);
  curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);

  res = curl_easy_perform(curl);
  
  if (res == CURLE_OK) {
    long response_code;
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
    if (response_code == 200)
    {
      std::cout << "Hello";
      CURLcode ret;
      CURL *hnd;

      hnd = curl_easy_init();
      curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L);
      curl_easy_setopt(hnd, CURLOPT_URL, agent_name);
      curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
      curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
      curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
      curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
      curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 1L);
      curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);

      ret = curl_easy_perform(hnd);

      curl_easy_cleanup(hnd);
      hnd = NULL;

    }
    else { }
  }
  curl_easy_cleanup(curl);
  curl = NULL;

}

This prints Hello to stdout but does not run the curl POST request, so How would one do a nested curl request.

EDIT: I tried removing the if statement. So my question is how does one do two libcurl requests

Aucun commentaire:

Enregistrer un commentaire