如何在此请求中使用 libcurl 并获得 json 服务器应答 C++?
How to use libcurl with this request and get json server answer c++?
我有一些要求,我想在 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 \
从网络下载内容的基本功能是
#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 here and here
关于 CURLOPT 的更多信息 here
这将 return std::string
具有给定的输出,例如您的 json。然后你需要解析它。
要为 windows 安装 libcurl,您可以关注 this 线程
对于 linux cmake,您应该在 cmake 文件旁边添加
set(CURL_LIBRARY "-lcurl")
find_package(CURL REQUIRED)
include_directories(... ${CURL_INCLUDE_DIR})
target_link_libraries(... $(CURL_LIBRARIES))
我有一些要求,我想在 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 \
从网络下载内容的基本功能是
#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 here and here
关于 CURLOPT 的更多信息 here
这将 return std::string
具有给定的输出,例如您的 json。然后你需要解析它。
要为 windows 安装 libcurl,您可以关注 this 线程
对于 linux cmake,您应该在 cmake 文件旁边添加
set(CURL_LIBRARY "-lcurl")
find_package(CURL REQUIRED)
include_directories(... ${CURL_INCLUDE_DIR})
target_link_libraries(... $(CURL_LIBRARIES))