-2

I have some requests and i want to usen them in libcurl. But i dont know how to do this So whot should i do implemete this in code like "curl.get(dsds) curl.header("", "")"

curl "https://pterodactyl.file.properties/api/client/account" \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer apikey' \
  -X GET \

curl "https://pterodactyl.file.properties/api/client/account/email" \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer apikey' \
  -X PUT \
  -d '{
  "email": "example@xample.com",
  "password": "Password"
}' 

curl "https://pterodactyl.file.properties/api/client/account/api-keys" \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer apikey' \
  -X POST \
  -d '{
  "description": "Restricted IPs",
  "allowed_ips": ["127.0.0.1", "192.168.0.1"]
}' 

curl "https://pterodactyl.file.properties/api/client/account/api-keys/NWKMYMT2Mrav0Iq2" \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer apikey' \
  -X DELETE \
Leopold95
  • 15
  • 6
  • 1
    Create a program that either compiles libcurl or links against it, then read through the documentation and samples to learn how to make those requests. They aren't difficult, but if you run into issues you can ask those specific questions here. – Retired Ninja Jun 19 '21 at 11:24

1 Answers1

1

Basic function for downloading something from web is

#include <iostream>
#include<curl/curl.h>
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}
std::string curlDownload(std::string link){
    CURL* curl;
    CURLcode res;
    std::string readBuffer;
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, link.c_str());
        curl_easy_setopt(curl, CURLOPT_USERNAME, "myUserName"); //auth for userName
        curl_easy_setopt(curl, CURLOPT_PASSWORD, "Password"); //auth for password
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }
    return readBuffer;
}
int main() {

    std::string myLink = "https://pterodactyl.file.properties/api/client/account";
    std::cout << curlDownload(myLink) << std::endl; //this will print your request for authorization

    return 0;
}

In order to put stuff you will need something like this, with your own data ofcourse

curl = curl_easy_init();

if (curl) {
    headers = curl_slist_append(headers, client_id_header);
    headers = curl_slist_append(headers, "Content-Type: application/json");

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); 
    curl_easy_setopt(curl, CURLOPT_URL, request_url);  
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); /* !!! */

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_struct); /* data goes here */

    res = curl_easy_perform(curl);

    curl_slist_free_all(headers);
    curl_easy_cleanup(curl);
}

More about put here and here

More about CURLOPT here This will return std::string with given output, your json for example. Then you need to parse it.

In order to install libcurl for windows, you can follow this thread

For linux cmake you should add next to your cmake file

set(CURL_LIBRARY "-lcurl")
find_package(CURL REQUIRED)
include_directories(... ${CURL_INCLUDE_DIR})
target_link_libraries(... $(CURL_LIBRARIES))
Dusan Todorovic
  • 472
  • 2
  • 12