L
L
Limons2021-12-07 17:38:25
C++ / C#
Limons, 2021-12-07 17:38:25

How to make post request PNG(base64) using curlcpp?

Please do not pay attention to unnecessary libraries and variables. I'd like to hear recommendations and bugs about the curl library. Am I doing it right?

I will send a screenshot of the captcha to the RuCaptcha server.
Post comes and I get the ID of my captcha, but the site catches:
61af71b51458d178953801.png

Here is the code:

#define CURL_STATICLIB

#include <iostream>
#include <sstream>
#include <curl/curl.h>
#include <string>
#include "base64.h"
#include <fstream>
#include <vector>
#include <cstdio>
using namespace std;



const string URL_in = "http://rucaptcha.com/in.php";

const string URL_res = "http://rucaptcha.com/res.php";


const string file_path = "C:\\png\\captcha\\captcha.png";

const string key = "xxx";
const string method = "base64";
const string phrase = "0";
const string regsense = "0";
const string numeric = "3";
const string calc = "0";
const string min_len = "1";
const string max_len = "4";
const string language = "2";

/*		+
    "&phrase=" + phrase +
    "&regsense=" + regsense +
    "&numeric=" + numeric +
    "&calc=" + calc +
    "&min_len=" + min_len +
    "&max_len=" + max_len +
    "&language=" + language;
*/

string base64_encode_image(const string& path) {
  vector<char> temp;

  ifstream infile;
  infile.open(path, ios::binary);    

  if (infile.is_open()) {
    while (!infile.eof()) {
      char c = (char)infile.get();
      temp.push_back(c);
    }
    infile.close();
  }

  else return "File could not be opened";
  string ret(temp.begin(), temp.end() - 1);
  ret = base64_encode((unsigned const char*)ret.c_str(), ret.size());

  return ret;
}

int main() {

  string data =
    "key=" + key +
    "&method=" + method +
    "&body=" + base64_encode_image(file_path);



  CURL* curl;
  CURLcode res;

  curl_global_init(CURL_GLOBAL_ALL);

  curl = curl_easy_init();

  if (curl) {

    curl_easy_setopt(curl, CURLOPT_URL, URL_in.c_str());
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()
    );

    res = curl_easy_perform(curl);

    if (res != CURLE_OK) {
      fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
    }

    curl_easy_cleanup(curl);

  


  }

  curl_global_cleanup();
  return 0;


}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question