在 Casablanca 中设置基本 HTTP 身份验证
Set Basic HTTP Authentication in Casablanca
我正在尝试修改 Casablanca tutorial to include basic HTTP authentication to access the Prosper API:
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
auto requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client_config config;
credentials creds( "username", "password" );
config.set_credentials( creds );
http_client client( U( "https://api.prosper.com/" ), config );
// Build request URI and start the request.
uri_builder builder(U("/api/Listings/"));
return client.request( methods::GET, builder.to_string() );
})
...
不幸的是,我一直收到错误 401 - 未经授权。但是,我可以通过 https://username:password@api.prosper.com/api/Listings/
在浏览器中访问该页面,并且我可以使用 Casablanca 访问不需要身份验证的常规网页。
一般来说,我对 REST 和 Web 内容不熟悉,文档毫无用处 - http_client_config
"used to set the possible configuration options." 不是开玩笑。我什至不确定我是否使用了正确的 类 - 这些东西看起来有点正确。
如何在 Casablanca 中向 http_client 请求添加基本身份验证?
您需要在包含 "username:password"
的 base64
的请求中添加 header
例如
// Please check how to convert into base64
XYZtaW46Wr6yZW0xMAXY = base64("username:password")
// Creating http_client
http_client_config config;
credentials cred(L"username", L"Password");
config.set_credentials(cred);
http_client client(U("https://serverip"),config);
// create header
http_request req(methods::GET);
// Add base64 result to header
req.headers().add(L"Authorization", L"Basic XYZtaW46Wr6yZW0xMAXY");
req.set_request_uri(L"/api/json/xyz");
pplx::task<http_response> responses = client.request(req);
pplx::task<web::json::value> jvalue = responses.get().extract_json();
wcout << jvalue.get().to_string();
我正在尝试修改 Casablanca tutorial to include basic HTTP authentication to access the Prosper API:
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
auto requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client_config config;
credentials creds( "username", "password" );
config.set_credentials( creds );
http_client client( U( "https://api.prosper.com/" ), config );
// Build request URI and start the request.
uri_builder builder(U("/api/Listings/"));
return client.request( methods::GET, builder.to_string() );
})
...
不幸的是,我一直收到错误 401 - 未经授权。但是,我可以通过 https://username:password@api.prosper.com/api/Listings/
在浏览器中访问该页面,并且我可以使用 Casablanca 访问不需要身份验证的常规网页。
一般来说,我对 REST 和 Web 内容不熟悉,文档毫无用处 - http_client_config
"used to set the possible configuration options." 不是开玩笑。我什至不确定我是否使用了正确的 类 - 这些东西看起来有点正确。
如何在 Casablanca 中向 http_client 请求添加基本身份验证?
您需要在包含 "username:password"
的 base64
的请求中添加 header
例如
// Please check how to convert into base64
XYZtaW46Wr6yZW0xMAXY = base64("username:password")
// Creating http_client
http_client_config config;
credentials cred(L"username", L"Password");
config.set_credentials(cred);
http_client client(U("https://serverip"),config);
// create header
http_request req(methods::GET);
// Add base64 result to header
req.headers().add(L"Authorization", L"Basic XYZtaW46Wr6yZW0xMAXY");
req.set_request_uri(L"/api/json/xyz");
pplx::task<http_response> responses = client.request(req);
pplx::task<web::json::value> jvalue = responses.get().extract_json();
wcout << jvalue.get().to_string();