user8520480
user8520480

Reputation:

Making a cURL GET request using pre-defined variables in C++

I am currently writing a C++ program on macOS which requires us to take two variables from the user, being the HWID and the IP address and use them in a get request like so;

CURL* curl;
string result;

curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "website.com/c.php?ip=" + ip + "&hwid=" + hwid);

This is how hwid and ip are defined;

auto hwid = al.exec("ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { print $3; }'");

auto ip = al.exec("dig +short myip.opendns.com @resolver1.opendns.com.");

Keep in mind al.exec is just a function that executes and returns the output of a terminal command.

However the issue with doing all of this, is I'm giving curl_easy_setopt incorrect types for params... I'm getting these errors when making the GET request like the example earlier;

Cannot pass object of non-trivial type 'basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >' through variadic function; call will abort at runtime

Any help would be greatly appreciated.

Upvotes: 0

Views: 1337

Answers (2)

user0042
user0042

Reputation: 8018

You should prepare a const char* to call curl_easy_setopt():

std::ostringstream oss;
oss << "website.com/c.php?ip=" << ip << "&hwid=" << hwid;
std::string url = oss.str();
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409364

The cURL library is a C library, all its functions are C functions. Therefore they can't handle object like std::string. And when you do "website.com/c.php?ip=" + ip + "&hwid=" + hwid the result is a std::string object.

One way to solve it is to save the result of "website.com/c.php?ip=" + ip + "&hwid=" + hwid in a variable, and then use that variable with the c_str function to get a C-style string:

std::string url = "website.com/c.php?ip=" + ip + "&hwid=" + hwid;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

Upvotes: 2

Related Questions