Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
C++ Requests - Curl for People
Search
whoshuu
September 28, 2015
Programming
430
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
C++ Requests - Curl for People
Lightning talk at CppCon 2015
https://github.com/whoshuu/cpr
whoshuu
September 28, 2015
Other Decks in Programming
See All in Programming
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
140
タクシーアプリ『GO』の バックエンド開発のおける AI利活用と若者のすべて
pyama86
3
1.9k
Lessons from Spec-Driven Development
simas
PRO
0
140
Swiftのレキシカルスコープ管理
kntkymt
0
210
AIとASP.NET Coreで雑Webアプリを作った話
mayuki
0
350
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
760
Composerを使ったサプライチェーン攻撃の様子を眺めてみる #phpstudy
o0h
PRO
2
220
Claspは野良GASの夢をみるか
takter00
0
170
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
310
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
230
AI時代の仕事技芸論 — ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ
kuranuki
1
620
Copilot CLI の継戦能力を高める コンテキスト管理
nozomutu
1
1.2k
Featured
See All Featured
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
410
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
370
Making the Leap to Tech Lead
cromwellryan
135
9.9k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
570
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
310
First, design no harm
axbom
PRO
2
1.2k
Six Lessons from altMBA
skipperchong
29
4.3k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
550
From π to Pie charts
rasagy
0
200
Building Applications with DynamoDB
mza
96
7.1k
Transcript
None
github.com/whoshuu/cpr
HTTP at a high level v Verbs v GET me
this page v PUT this data here v DELETE this user v That’s it
Isn’t that naïve? v Authentication v Headers v Cookies,
sessions, proxies, etc.
HTTP should be as simple as a print statement
-‐ Kenneth Reitz
Let’s try it
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto r = cpr::Get(Url{“https://httpbin.org/get”}); std::cout << r.text << std::endl; }
Compare this to
#include <curl/curl.h> #include <iostream> #include <string> size_t write(void* ptr, size_t
size, size_t nmemb, std::string* data) { data->append((char*) ptr, size * nmemb); return size * nmemb; } int main(int argc, char** argv) { auto curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, “https://httpbin.org/get”}); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L); curl_easy_setopt(curl, CURLOPT_USERAGENT, “curl/7.42.0”); curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L); std::string response_text; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_text); curl_easy_perform(curl); curl_easy_cleanup(curl); curl = NULL; std::cout << response_text << std::endl; }
And that’s the easy interface
#include <Poco/Net/......> #include <iostream> #include <string> int main(int argc, char**
argv) { Poco::URI uri(“https://httpbin.org/get”); Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort()); std::string path(uri.getPathAndQuery()); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, path, Poco::Net::HTTPMessage::HTTP_1_1); session.sendRequest(request); Poco::Net::HTTPResponse response; std::istream& in_stream = session.receiveResponse(response); std::ostringstream out_stream; Poco::StreamCopier::copyStream(in_stream, out_stream); std::cout << out_stream.str() << std::endl; }
Seven nouns participating in several APIs
uri : getHost(), getPort(), getPathAndQuery() session : sendRequest(), receiveResponse() path
: request constructor request : sendRequest() response : receiveResponse() in_stream : copy_stream() out_stream : copy_stream()
An HTTP API for People v Objects participate in a
single request verb v Steps to response are minimal v Is useful in 95% of use cases
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto r = cpr::Get(Url{“https://httpbin.org/get”}, Parameters{{“key”, “value”}}); std::cout << r.text << std::endl; } Query parameters
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto r = cpr::Get(Url{“https://httpbin.org/get”}, Authentication{“user”, “pass”}); std::cout << r.text << std::endl; } Authentication
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto r = cpr::Get(Url{“https://httpbin.org/get”}, Digest{“user”, “pass”}); std::cout << r.text << std::endl; } Digest Authentication
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto r = cpr::Get(Url{“https://httpbin.org/get”}, Headers{{“Accept”, “test/html”}}); std::cout << r.text << std::endl; } Custom headers
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto r = cpr::Post(Url{“https://httpbin.org/post”}, Payload{{“key”, “value”}, {“hello”, “world”}}); std::cout << r.text << std::endl; } Url encoded POSTs
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto r = cpr::Post(Url{“https://httpbin.org/post”}, Multipart{{“key”, “long-string”}}); std::cout << r.text << std::endl; } Multipart POSTs
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto r = cpr::Post(Url{“https://httpbin.org/post”}, Multipart{{“key”, “long-string”}, {“name”, File{“path”}}}); std::cout << r.text << std::endl; } File POSTs
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto r = cpr::Get(Url{“https://httpbin.org/post”}, Authentication{“user”, “pass”}, Headers{{“Accept”, “test/html”}}, Parameters{{“key”, “value”}}); std::cout << r.text << std::endl; } More complex requests
And with some template magic...
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto r = cpr::Get(Url{“https://httpbin.org/get”}, Authentication{“user”, “pass”}, Headers{{“Accept”, “test/html”}}, Parameters{{“key”, “value”}}); std::cout << r.text << std::endl; } Orderless arguments
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto r = cpr::Get(Url{“https://httpbin.org/get”}, Headers{{“Accept”, “test/html”}}, Authentication{“user”, “pass”}, Parameters{{“key”, “value”}}); std::cout << r.text << std::endl; } Orderless arguments
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto r = cpr::Get(Url{“https://httpbin.org/get”}, Headers{{“Accept”, “test/html”}}, Parameters{{“key”, “value”}}, Authentication{“user”, “pass”}); std::cout << r.text << std::endl; } Orderless arguments
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto r = cpr::Get(Url{“https://httpbin.org/get”}, Authentication{“user”, “pass”}, Parameters{{“key”, “value”}}, Headers{{“Accept”, “test/html”}}); std::cout << r.text << std::endl; } Orderless arguments
An API is a rung on an abstraction ladder
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
Session session; session.SetUrl(“https://httpbin.org/get”); session.SetParameters({{“key”, “value”}}); auto r = session.Get(); std::cout << r.text << std::endl; } Sessions
#include <cpr.h> #include <iostream> #include <future> #include <vector> int main(int
argc, char** argv) { std::vector<std::future<Response>> responses; auto url = Url{“https://httpbin.org/get”}; for (int i = 0; i < 1000; ++i) { responses.emplace_back(cpr::GetAsync(url)); } for (auto& r : responses) { std::cout << r.get().text << std::endl; } } Asynchronous requests
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto future = cpr::GetCallback([](Response r) { return r.text; }, Url{“https://httpbin.org/get”}); std::cout << future.get() << std::endl; } Asynchronous callbacks
#include <cpr.h> #include <iostream> int main(int argc, char** argv) {
auto r = cpr::Get(Url{“https://httpbin.org/get”}); std::cout << r.text << std::endl; }
Developers are People too
This is an API for People
None
github.com/whoshuu/cpr
Thanks.