C++ Requests - 现代、简洁、易用的 HTTP 客户端库
CPR(C++ Requests)是一个现代化的 C++ HTTP 库,灵感来源于 Python 的 Requests 库。它基于 libcurl 构建,提供了简洁、直观的 API,让 HTTP 请求变得异常简单。
类 Python Requests 的直观 API
支持 Windows、Linux、macOS
支持所有 HTTP 方法
支持 HTTPS 和证书验证
vcpkg install cpr
conan install cpr/1.10.0@
brew install cpr
git clone https://github.com/libcpr/cpr.git
cd cpr
mkdir build && cd build
cmake .. -DCPR_BUILD_TESTS=OFF
cmake --build . --parallel
sudo cmake --install .
#include <iostream>
#include <cpr/cpr.h>
int main() {
cpr::Response r = cpr::Get(cpr::Url{"https://httpbin.org/get"});
std::cout << "Status: " << r.status_code << std::endl;
std::cout << "Content: " << r.text << std::endl;
return 0;
}
cpr::Response r = cpr::Get(
cpr::Url{"https://httpbin.org/get"},
cpr::Parameters{
{"name", "John"},
{"age", "25"},
{"city", "Beijing"}
}
);
#include <cpr/cpr.h>
int main() {
cpr::Response r = cpr::Post(
cpr::Url{"https://httpbin.org/post"},
cpr::Header{{"Content-Type", "application/json"}},
cpr::Body{R"({"name":"John","age":25})"}
);
std::cout << r.text << std::endl;
return 0;
}
cpr::Response r = cpr::Post(
cpr::Url{"https://httpbin.org/post"},
cpr::Payload{
{"username", "admin"},
{"password", "123456"}
}
);
cpr::Response r = cpr::Post(
cpr::Url{"https://httpbin.org/post"},
cpr::Multipart{
{"file", cpr::File{"./test.txt"}},
{"description", "Test file"}
}
);
#include <cpr/cpr.h>
#include <iostream>
int main() {
// 异步请求
auto future = cpr::GetAsync(
cpr::Url{"https://httpbin.org/get"}
);
// 在这里可以做其他事情...
// 获取结果
cpr::Response r = future.get();
std::cout << r.status_code << std::endl;
return 0;
}
cpr::Response r = cpr::Get(
cpr::Url{"https://httpbin.org/get"},
cpr::Timeout{3000} // 3秒超时
);
cpr::Response r = cpr::Get(
cpr::Url{"https://api.example.com/data"},
cpr::Header{
{"Authorization", "Bearer your_token_here"},
{"User-Agent", "MyApp/1.0"},
{"Accept", "application/json"}
}
);
// 使用 Cookie
cpr::Response r = cpr::Get(
cpr::Url{"https://httpbin.org/cookies"},
cpr::Cookies{{"name", "John"}, {"age", "25"}}
);
// 保存 Cookie
cpr::Cookies cookies;
cpr::Response r = cpr::Get(
cpr::Url{"https://example.com"},
cpr::Cookies{cookies}
);
cpr::Response r = cpr::Get(
cpr::Url{"https://example.com"},
cpr::Ssl(ssl::TLSv1_2{}, ssl::NoVerify{true})
);
#include <cpr/cpr.h>
int main() {
// 创建会话
cpr::Session session;
// 设置 URL 和参数
session.SetUrl(cpr::Url{"https://httpbin.org/get"});
session.SetParameters({{"name", "John"}});
// 发送请求
cpr::Response r = session.Get();
std::cout << r.text << std::endl;
return 0;
}
#include <iostream>
#include <cpr/cpr.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
const std::string baseUrl = "https://jsonplaceholder.typicode.com";
// 1. GET - 获取所有帖子
std::cout << "=== 获取所有帖子 ===" << std::endl;
cpr::Response r = cpr::Get(
cpr::Url{baseUrl + "/posts"},
cpr::Timeout{5000}
);
std::cout << "Status: " << r.status_code << std::endl;
// 2. GET - 获取单个帖子
std::cout << "\n=== 获取单个帖子 ===" << std::endl;
r = cpr::Get(cpr::Url{baseUrl + "/posts/1"});
std::cout << r.text << std::endl;
// 3. POST - 创建新帖子
std::cout << "\n=== 创建新帖子 ===" << std::endl;
json newPost = {
{"title", "foo"},
{"body", "bar"},
{"userId", 1}
};
r = cpr::Post(
cpr::Url{baseUrl + "/posts"},
cpr::Header{{"Content-Type", "application/json"}},
cpr::Body{newPost.dump()}
);
std::cout << r.text << std::endl;
// 4. PUT - 更新帖子
std::cout << "\n=== 更新帖子 ===" << std::endl;
json updatedPost = {
{"title", "updated title"},
{"body", "updated body"}
};
r = cpr::Put(
cpr::Url{baseUrl + "/posts/1"},
cpr::Header{{"Content-Type", "application/json"}},
cpr::Body{updatedPost.dump()}
);
std::cout << r.text << std::endl;
// 5. DELETE - 删除帖子
std::cout << "\n=== 删除帖子 ===" << std::endl;
r = cpr::Delete(cpr::Url{baseUrl + "/posts/1"});
std::cout << "Status: " << r.status_code << std::endl;
return 0;
}
| 方法 | 用途 | CPR 示例 |
|---|---|---|
GET |
获取数据 | cpr::Get(url) |
POST |
创建资源 | cpr::Post(url, body) |
PUT |
更新资源 | cpr::Put(url, body) |
PATCH |
部分更新 | cpr::Patch(url, body) |
DELETE |
删除资源 | cpr::Delete(url) |
HEAD |
获取头部 | cpr::Head(url) |
find_package(CPR REQUIRED) 来在 CMake 项目中集成 CPR。