Back

cpr请求库

本文介绍的HTTP库👉libcpr/cpr: C++ Requests: Curl for People, a spiritual port of Python Requests. (github.com)

HttpServer

Url

Url url{server->GetBaseUrl() + "/hello.html"};

Url url{server->GetBaseUrl() + "/basic.json"};

Url url{server->GetBaseUrl() + "/error.html"};

Url url{"http://bad_host/"};

Url url{server->GetBaseUrl() + "/basic_cookies.html"};

Url url{server->GetBaseUrl() + "/empty_cookies.html"};

Url url{server->GetBaseUrl() + "/check_cookies.html"};

Url url{server->GetBaseUrl() + "/v1_cookies.html"};

Url url{server->GetBaseUrl() + "/check_v1_cookies.html"};

Url url{server->GetBaseUrl() + "/basic_auth.html"};

Url url{server->GetBaseUrl() + "/bearer_token.html"};

Url url{server->GetBaseUrl() + "/digest_auth.html"};

Url url{server->GetBaseUrl() + "/header_reflect.html"};

Url url{server->GetBaseUrl() + "/temporary_redirect.html"};

Url url{server->GetBaseUrl() + "/permanent_redirect.html"};

Url url{server->GetBaseUrl() + "/body_get.html"};

Get

Response response = cpr::Get(url)

Response response = cpr::Get(url, iface);

Response response = cpr::Get(url, Timeout{0L});

Response response = cpr::Get(url, cookies);

Response response = cpr::Get(url, parameters{});

Response response = cpr::Get(url, Authentication{});

Response response = cpr::Get(url, Header{});

Response response = cpr::Get(url, Bearer{});

Response response = cpr::Get(url, Digest{});

Response response = cpr::Get(url, Authentication{}, Parameters{});

Response response = cpr::Get(url, Parameters{}, Authentication{});

Response response = cpr::Get(url, Authentication{}, Header{});

Response response = cpr::Get(url, Authentication{}, Header{});

Response response = cpr::Get(url, Header{}, Authentication{});

Response response = cpr::Get(url, Header{}, Parameters{});

Response response = cpr::Get(url, Parameters{}, Header{});

Response response = cpr::Get(url, Authentication{}, Parameters{}, Header{});

Response response = cpr::Get(url, Parameters{}, Header{}, Authentication{});

Response response = cpr::Get(url, Header{}, Authentication{}, Parameters{});

Response response = cpr::Get(url, Authentication{}, Header{}, Parameters{});

Response response = cpr::Get(url, Parameters{}, Authentication{}, Header{});

Response response = cpr::Get(url, Redirect(false));

Response response = cpr::Get(url, Redirect(0L));

Response response = cpr::Get(url, Authentication{}, Header{}, Redirect(true, true));

Response response = cpr::Get(url, body);

Response response = cpr::Get(url, LimitRate(1024, 1024));

Response response = cpr::Get(url);

Response response = cpr::Get(url, iface);

Response response = cpr::Get(url, Timeout{0L});

Response response = cpr::Get(url, cookies);

Response response = cpr::Get(url, parameters);

Response response = cpr::Get(url, Parameters{{"key", "value"}, {"hello", "world"}, {"test", "case"}});

Response response = cpr::Get(url, Authentication{"user", "password"});

Response response = cpr::Get(url, Header{{"Authorization", "Bearer the_token"}});

Response response = cpr::Get(url, Bearer{"the_token"});

Response response = cpr::Get(url, Digest{"user", "password"});

Response response = cpr::Get(url, Authentication{"user", "password"}, Parameters{{"hello", "world"}});

Response response = cpr::Get(url, Authentication{"user", "password"}, Parameters{{"key", "value"}, {"hello", "world"}, {"test", "case"}});

Response response = cpr::Get(url, Parameters{{"hello", "world"}}, Authentication{"user", "password"});

Response response = cpr::Get(url, Parameters{{"key", "value"}, {"hello", "world"}, {"test", "case"}}, Authentication{"user", "password"});

Response response = cpr::Get(url, Authentication{"user", "password"}, Header{{"hello", "world"}});

Response response = cpr::Get(url, Authentication{"user", "password"}, Header{{"key", "value"}, {"hello", "world"}, {"test", "case"}});

Response response = cpr::Get(url, Header{{"hello", "world"}}, Authentication{"user", "password"});

Response response = cpr::Get(url, Header{{"key", "value"}, {"hello", "world"}, {"test", "case"}}, Authentication{"user", "password"});

Response response = cpr::Get(url, Authentication{"user", "bad_password"});

Response response = cpr::Get(url, Authentication{"user", "bad_password"}, Parameters{{"hello", "world"}});

Response response = cpr::Get(url, Authentication{"user", "bad_password"}, Parameters{{"key", "value"}, {"hello", "world"}, {"test", "case"}});

Response response = cpr::Get(url, Header{{"content-type", "application/json"}});

Response = session.Get();

cpr::Response response = session->Post();

Response response = session.Get();

Response response = cpr::Get(url, Header{});

Response response = cpr::Get(url, Header{{"hello", "world"}});

Response response = cpr::Get(url, Header{{"hello", "world"}, {"key", "value"}, {"test", "case"}});

Response response = cpr::Get(url, Header{{"HeLlO", "wOrLd"}});

Response response = cpr::Get(url, Header{{"hello", ""}});

Response response = cpr::Get(url, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}});

Response response = cpr::Get(url, Header{}, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}});

Response response = cpr::Get(url, Header{{"hello", "world"}}, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}});

Response response = cpr::Get(url, Header{{"hello", "world"}, {"key", "value"}, {"test", "case"}}, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}});

Response response = cpr::Get(url, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}}, Header{});

Response response = cpr::Get(url, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}}, Header{{"hello", "world"}});
Response response = cpr::Get(url, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}}, Header{{"hello", "world"}, {"key", "value"}, {"test", "case"}});
Response response = cpr::Get(url, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}}, Header{{"HeLlO", "wOrLd"}});

Response response = cpr::Get(url, Authentication{"user", "password"}, Parameters{}, Header{});
Response response = cpr::Get(url, Authentication{"user", "bad_password"}, Parameters{}, Header{});

Response response = cpr::Get(url, Authentication{"user", "password"}, Parameters{{"one", "two"}}, Header{});
Response response = cpr::Get(url, Authentication{"user", "bad_password"}, Parameters{{"one", "two"}}, Header{});

Response response = cpr::Get(url, Authentication{"user", "password"}, Parameters{}, Header{{"hello", "world"}});
Response response = cpr::Get(url, Authentication{"user", "bad_password"}, Parameters{}, Header{{"hello", "world"}});

Response response = cpr::Get(url, Authentication{"user", "password"}, Parameters{{"one", "two"}}, Header{{"hello", "world"}});
Response response = cpr::Get(url, Authentication{"user", "bad_password"}, Parameters{{"one", "two"}}, Header{{"hello", "world"}});

Response response = cpr::Get(url, Parameters{}, Header{}, Authentication{"user", "password"});
Response response = cpr::Get(url, Parameters{}, Header{}, Authentication{"user", "bad_password"});

Response response = cpr::Get(url, Parameters{{"one", "two"}}, Header{}, Authentication{"user", "password"});
Response response = cpr::Get(url, Parameters{{"one", "two"}}, Header{}, Authentication{"user", "bad_password"});

Response response = cpr::Get(url, Parameters{}, Header{{"hello", "world"}}, Authentication{"user", "password"});
Response response = cpr::Get(url, Parameters{}, Header{{"hello", "world"}}, Authentication{"user", "bad_password"});

Response response = cpr::Get(url, Parameters{{"one", "two"}}, Header{{"hello", "world"}}, Authentication{"user", "password"});
Response response = cpr::Get(url, Parameters{{"one", "two"}}, Header{{"hello", "world"}}, Authentication{"user", "bad_password"});

Response response = cpr::Get(url, Header{}, Authentication{"user", "password"}, Parameters{});
Response response = cpr::Get(url, Header{}, Authentication{"user", "bad_password"}, Parameters{});

Response response = cpr::Get(url, Header{}, Authentication{"user", "password"}, Parameters{{"one", "two"}});
Response response = cpr::Get(url, Header{}, Authentication{"user", "bad_password"}, Parameters{{"one", "two"}});

Response response = cpr::Get(url, Header{{"hello", "world"}}, Authentication{"user", "password"}, Parameters{});
Response response = cpr::Get(url, Header{{"hello", "world"}}, Authentication{"user", "bad_password"}, Parameters{});

Response response = cpr::Get(url, Header{{"hello", "world"}}, Authentication{"user", "password"}, Parameters{{"one", "two"}});
Response response = cpr::Get(url, Header{{"hello", "world"}}, Authentication{"user", "bad_password"}, Parameters{{"one", "two"}});

Response response = cpr::Get(url, Authentication{"user", "password"}, Header{}, Parameters{});
Response response = cpr::Get(url, Authentication{"user", "bad_password"}, Header{}, Parameters{});

Response response = cpr::Get(url, Authentication{"user", "password"}, Header{}, Parameters{{"one", "two"}});
Response response = cpr::Get(url, Authentication{"user", "bad_password"}, Header{}, Parameters{{"one", "two"}});

Response response = cpr::Get(url, Authentication{"user", "password"}, Header{{"hello", "world"}}, Parameters{});
Response response = cpr::Get(url, Authentication{"user", "bad_password"}, Header{{"hello", "world"}}, Parameters{});

Response response = cpr::Get(url, Authentication{"user", "password"}, Header{{"hello", "world"}}, Parameters{{"one", "two"}});
Response response = cpr::Get(url, Authentication{"user", "bad_password"}, Header{{"hello", "world"}}, Parameters{{"one", "two"}});

Response response = cpr::Get(url, Header{}, Parameters{}, Authentication{"user", "password"});
Response response = cpr::Get(url, Header{}, Parameters{}, Authentication{"user", "bad_password"});

Response response = cpr::Get(url, Header{}, Parameters{{"one", "two"}}, Authentication{"user", "password"});
Response response = cpr::Get(url, Header{}, Parameters{{"one", "two"}}, Authentication{"user", "bad_password"});

Response response = cpr::Get(url, Header{{"hello", "world"}}, Parameters{}, Authentication{"user", "password"});
Response response = cpr::Get(url, Header{{"hello", "world"}}, Parameters{}, Authentication{"user", "bad_password"});

Response response = cpr::Get(url, Header{{"hello", "world"}}, Parameters{{"one", "two"}}, Authentication{"user", "password"});
Response response = cpr::Get(url, Header{{"hello", "world"}}, Parameters{{"one", "two"}}, Authentication{"user", "bad_password"});

Response response = cpr::Get(url, Parameters{}, Authentication{"user", "password"}, Header{});
Response response = cpr::Get(url, Parameters{}, Authentication{"user", "bad_password"}, Header{});

Response response = cpr::Get(url, Parameters{{"one", "two"}}, Authentication{"user", "password"}, Header{});
Response response = cpr::Get(url, Parameters{{"one", "two"}}, Authentication{"user", "bad_password"}, Header{});

Response response = cpr::Get(url, Parameters{}, Authentication{"user", "password"}, Header{{"hello", "world"}});
Response response = cpr::Get(url, Parameters{}, Authentication{"user", "bad_password"}, Header{{"hello", "world"}});

Response response = cpr::Get(url, Parameters{{"one", "two"}}, Authentication{"user", "password"}, Header{{"hello", "world"}});
Response response = cpr::Get(url, Parameters{{"one", "two"}}, Authentication{"user", "bad_password"}, Header{{"hello", "world"}});

Response response = cpr::Get(url, Redirect(false));

Response response = cpr::Get(url, Redirect(0L));

Response response = cpr::Get(url, Authentication{"user", "password"}, Header{{"RedirectLocation", "basic_auth.html"}}, Redirect(true, true));

Response response = cpr::Get(url, body);

Response response = cpr::Get(url, LimitRate(1024, 1024));

cpr::Post

Response response = cpr::Post(url, Payload{{"x", "5"}});

Payload payload{{"x", "1"}};
    payload.Add({"y", "2"});
    Response response = cpr::Post(url, payload);

std::vector<Pair> payloadData;
    payloadData.emplace_back("x", "1");
    payloadData.emplace_back("y", "2");
    Response response = cpr::Post(url, Payload(payloadData.begin(), payloadData.end()));
    
Response response = cpr::Post(url, Payload{{"x", "hello world!!~"}});

Response response = cpr::Post(url, Payload{{"x", "5"}, {"y", "13"}});

Url url{"http://bad_host/"};
    Response response = cpr::Post(url, Payload{{"hello", "world"}});
    
Response response = cpr::Post(url, Multipart{{"x", 5}});

Response response = cpr::Post(url, Multipart{{"x", File{filename}}});

Multipart multipart{{"x", File{filename}}};
Response response = cpr::Post(url, multipart);

cpr::Response response = cpr::Post(url, cpr::Header{{"Content-Type", "application/json"}}, cpr::Body{body}, cpr::ConnectTimeout{3000}, cpr::Timeout{3000});

Response response = cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});

Multipart multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}};
Response response = cpr::Post(url, multipart);

Response response = cpr::Post(url, Multipart{{"x", Buffer{content, 11 + content, "test_file"}}});

Response response = cpr::Post(url, Multipart{{"x", Buffer{std::begin(content), std::end(content) - 1, "test_file"}}});

Response response = cpr::Post(url, Multipart{{"x", 5}, {"y", 13}});

Response response = cpr::Post(url, multipart);

Response response = cpr::Post(url, Multipart{{"x", 5, "application/number"}});

Multipart multipart{{"x", 5, "application/number"}};
Response response = cpr::Post(url, multipart);

Response response = cpr::Post(cpr::Url(uri), cpr::Header{{"content-type", contentType}, {"Authorization", signature}, {"log-type", logType}, {"x-ms-date", date}, {"content-length", std::to_string(body.length())}}, cpr::Body(body));

cpr::Payload payload = cpr::Payload{{"email", ""}, {"password", ""}, {"devicetoken", ""}};
    cpr::Response response = cpr::Post(cpr::Url(uri), cpr::Timeout{10000}, payload);

Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}});

Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}}, Redirect(PostRedirectFlags::NONE));

Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}});

Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}}, Redirect(PostRedirectFlags::NONE));

std::string expected_text{""};

std::string expected_text{"Hello world!"};

std::string expected_text{
            "[\n"
            "  {\n"
            "    \"first_key\": \"first_value\",\n"
            "    \"second_key\": \"second_value\"\n"
            "  }\n"
            "]"};

std::string expected_text{"Not Found"};

std::string expected_text{"Header reflect GET"};

EXPECT_EQ()

EXPECT_EQ(expected_text, response.text);

EXPECT_EQ(std::string{}, response.text);

EXPECT_EQ("Unauthorized", response.text);

EXPECT_EQ(std::string{"Header reflect POST"}, response.text);

EXPECT_EQ(std::string{"Header reflect POST"}, response.text);

EXPECT_EQ(url, response.url);

EXPECT_EQ("text/plain", response.header["content-type"]);

EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);

EXPECT_EQ(std::string{"application/json"}, response.header["Content-Type"]);

EXPECT_EQ(std::string{"Value1"}, response.header["Header1"]);

EXPECT_EQ(std::string{"Value2"}, response.header["Header2"]);

EXPECT_EQ(std::string{}, response.header["Header2"]);

cookies = response.cookies;
EXPECT_EQ(cookies["cookie"], response.cookies["cookie"]);
EXPECT_EQ(cookies["icecream"], response.cookies["icecream"]);
EXPECT_EQ(cookies["expires"], response.cookies["expires"]);

EXPECT_EQ("", response.cookies["cookie"]);
EXPECT_EQ("", response.cookies["icecream"]);

EXPECT_EQ(200, response.status_code);

EXPECT_EQ(401, response.status_code);

EXPECT_EQ(ErrorCode::OK, response.error.code);