Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
C++ Requests - Curl for People
Search
whoshuu
September 28, 2015
Programming
440
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
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
180
自サークルのIdPからログアウトできるようになるまで
taiseiue
1
140
PumaからFalconに移行したRailsアプリケーションでの実践
falcon8823
0
170
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Kaigi on Rails 2026, Tokyo, Japan
marcoroth
2
220
Findy - エンジニア向け会社紹介/Findy Company Deck
findyinc
6
400k
AWSに止められる覚悟してますか?
morizo_1984
2
540
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
790
Simple Storage Service(S3) is not simple
iwatsukayura
0
290
JIS keyboard support in Omarchy
lef237
0
120
[JA] Domain Modeling with Vanilla Rails: 「操作」から再考するRailsの設計論
yasaichi
0
140
golang-bob.pdf
iwatsukayura
0
130
複雑なUIコンポーネントとの ⻑い付き合い⽅:波及するバグからテスト戦略を経て、安全な再設計へ
mixplace
0
410
Featured
See All Featured
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
Optimising Largest Contentful Paint
csswizardry
37
4k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.9k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
600
Color Theory Basics | Prateek | Gurzu
gurzu
1
490
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
440
How to Ace a Technical Interview
jacobian
280
24k
Odyssey Design
rkendrick25
PRO
2
870
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
490
Marketing to machines
jonoalderson
1
5.9k
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.