【问题标题】:How to use libcurl with this request and get json server answer c++? [closed]如何在此请求中使用 libcurl 并获取 json server answer c++? [关闭]
【发布时间】:2021-09-03 20:21:42
【问题描述】:

我有一些请求,我想在 libcurl 中使用它们。但我不知道该怎么做 那么我应该在“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 \

【问题讨论】:

  • 创建一个程序来编译 libcurl 或链接它,然后通读文档和示例以了解如何提出这些请求。它们并不难,但如果您遇到问题,可以在这里提出这些具体问题。

标签: c++ libcurl


【解决方案1】:

从网上下载东西的基本功能是

#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;
}

为了放置东西,你需要这样的东西,当然还有你自己的数据

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);
}

更多关于 put herehere

更多关于 CURLOPT here 这将返回带有给定输出的std::string,例如您的json。然后你需要解析它。

为了安装libcurl for windows,你可以关注this线程

对于 linux cmake,你应该在你的 cmake 文件旁边添加

set(CURL_LIBRARY "-lcurl")
find_package(CURL REQUIRED)
include_directories(... ${CURL_INCLUDE_DIR})
target_link_libraries(... $(CURL_LIBRARIES))

【讨论】:

  • 哦,我非常感谢你!但是您能否展示如何正确地将标头和请求本身的类型添加到请求中,因为坦率地说,我无法理解如何正确执行此操作,因为 libcurl 对我来说看起来非常困难
  • @Leopold95 你能告诉我你希望在哪个操作系统上这样做吗?我会写下来
  • 现在我在 windows 下。但是vs2019中有qt项目。将来可能会在 linux 下构建这个 prog
  • 我用 c++ cmake linux 编辑答案
  • 所以我改变了门控你。一切正常。
猜你喜欢
  • 2012-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-05
  • 1970-01-01
  • 1970-01-01
  • 2014-02-22
  • 1970-01-01
相关资源
最近更新 更多