#libcurl post request

35 messages · Page 1 of 1 (latest)

polar harbor
#

Im creating an ImGui gui which should post JSON body but it doesnt send the request

ImGui::BeginChild("Config", ImVec2(0, 0), ImGuiChildFlags_Border);
{
    ImAdd::SeparatorText("Config Options");

    if (ImGui::Button("Load Config")) {
        std::cout << "Button clicked" << std::endl;
        nlohmann::json config;

        config["velocity"]["enabled"] = velocity::enabled;
        config["velocity"]["velocityV"] = velocity::velocityV;
        config["velocity"]["velocityH"] = velocity::velocityH;
        config["speed"]["enabled"] = speed::enabled;
        config["aim"]["enabled"] = aim::enabled;
        config["aim"]["aim_distance"] = aim::aim_distance;
        config["aim"]["aim_speed"] = aim::aim_speed;
        config["aim"]["max_angle"] = aim::max_angle;
        config["leftClicker"]["enabled"] = leftClicker::enabled;
        config["leftClicker"]["minCPS"] = leftClicker::minCPS;
        config["leftClicker"]["maxCPS"] = leftClicker::maxCPS;
        config["rightClicker"]["enabled"] = rightClicker::enabled;
        config["rightClicker"]["minCPS"] = rightClicker::minCPS;
        config["rightClicker"]["maxCPS"] = rightClicker::maxCPS;

        SendConfigToServer(config, "(uuid)");
    }
}```


SendConfigToServer function:
```cpp
void SendConfigToServer(const nlohmann::json& config, const std::string& player_uuid) {
    CURL* curl;
    CURLcode res;
    std::string url = "https://mydomain/api/config";


    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();

    if (curl) {
        
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

        std::string json_data = config.dump();
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_data.c_str());

        struct curl_slist* headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: application/json");

        std::string uuid_header = "x-player-uuid: " + player_uuid;
        headers = curl_slist_append(headers, uuid_header.c_str());

        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

        res = curl_easy_perform(curl);

        if (res != CURLE_OK) {
            std::cerr << "Request failed: " << curl_easy_strerror(res) << std::endl;
        }
        else {
            std::cout << "Config successfully sent!" << std::endl;
        }

        curl_easy_cleanup(curl);
        curl_slist_free_all(headers);
    }

    curl_global_cleanup();
}```
keen bluff
#

what's the issue?

polar harbor
#

It says "Config successfully sent!" But it didnt send lol

#

i tested the api using insomnia and it went through so something is wrong with that code

keen bluff
#

how do you know it didn't send?

polar harbor
#

im hosting on vercel and it logs every request

keen bluff
#

you sure you have the right domain?

polar harbor
#

yup

rich drumBOT
#

@polar harbor has reached level 1. GG!

polar harbor
#

pretty sure

keen bluff
#

have you tried a get request?

polar harbor
#

well my api itself looks like this

const configs = {};

module.exports = async (req, res) => {
    try {
        const uuid = req.headers["x-player-uuid"];

        if (!uuid) {
            return res.status(400).json({ error: "UUID is required" });
        }
    
        if (req.method === "POST") {
            const { config } = req.body;
            console.log(config)
    
            if (!config) {
                return res.status(400).json({ error: "No request body found" });
            }
    
            configs[uuid] = config;
            return res.status(200).json({ message: "Config saved!", config });
        } else if (req.method === "GET") {
            const config = configs[uuid];
    
            if (!config) {
                return res.status(404).json({ error: "Config for this uuid wasnt found!" });
            }
    
            return res.status(200).json({ config });
        } else {
            return res.status(405).json({ error: "Method not allowed." });
        }
    } catch (error) {
        console.log(error);
    }

};
#

it has an get and post request

#

Post to post the config and get to load the config

#

all of this works find when using insomnia to do the requests

#

so i havent really tried to do an get request on my imgui

keen bluff
#

if you do a get request what reponse do you get?

polar harbor
#

like if it goes through or if i do the get request on the imgui side?

keen bluff
#

the request reponse

#

on the cpp side

polar harbor
#

One moment

#

i dont get any response then

keen bluff
#

if you do a successful get request you should get some kind of response

#

so check the HTTP response code etc

polar harbor
#

Am I stupid rn or why does it not log anything?

size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

void getConfig(const std::string& player_uuid) {
    CURL* curl;
    CURLcode res;
    std::string url = "https://mydomain/externalConfig";
    std::string readBuffer;

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();

    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);

        struct curl_slist* headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: application/json");

        std::string uuid_header = "x-player-uuid: " + player_uuid;
        headers = curl_slist_append(headers, uuid_header.c_str());

        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);

        res = curl_easy_perform(curl);

        if (res != CURLE_OK) {
            std::cerr << "Request failed: " << curl_easy_strerror(res) << std::endl;
        }
        else {
            long http_code = 0;
            curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &http_code);

            std::cout << "Response body: " << readBuffer << std::endl;
            std::cout << "HTTP Status Code: " << http_code << std::endl;
        }

        curl_easy_cleanup(curl);
        curl_slist_free_all(headers);
    }

    curl_global_cleanup();
}```
#
getConfig("myuuid");```
polar harbor
#

i am

frail light
#

using jni?

polar harbor
polar harbor
#

how do i make it work with ssl?

keen bluff
#

Which version did you get?