Added convenience function Client::Response::Content::string()
This commit is contained in:
parent
4efdc095bd
commit
6b7fd993cf
5 changed files with 29 additions and 4 deletions
|
|
@ -37,20 +37,37 @@ namespace SimpleWeb {
|
||||||
template <class socket_type>
|
template <class socket_type>
|
||||||
class ClientBase {
|
class ClientBase {
|
||||||
public:
|
public:
|
||||||
|
class Content : public std::istream {
|
||||||
|
friend class ClientBase<socket_type>;
|
||||||
|
public:
|
||||||
|
size_t size() {
|
||||||
|
return streambuf.size();
|
||||||
|
}
|
||||||
|
/// Convenience function to return std::string. Note that the stream buffer is emptied when this functions is used.
|
||||||
|
std::string string() {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << rdbuf();
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
asio::streambuf &streambuf;
|
||||||
|
Content(asio::streambuf &streambuf): std::istream(&streambuf), streambuf(streambuf) {}
|
||||||
|
};
|
||||||
|
|
||||||
class Response {
|
class Response {
|
||||||
friend class ClientBase<socket_type>;
|
friend class ClientBase<socket_type>;
|
||||||
friend class Client<socket_type>;
|
friend class Client<socket_type>;
|
||||||
public:
|
public:
|
||||||
std::string http_version, status_code;
|
std::string http_version, status_code;
|
||||||
|
|
||||||
std::istream content;
|
Content content;
|
||||||
|
|
||||||
CaseInsensitiveMultimap header;
|
CaseInsensitiveMultimap header;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
asio::streambuf content_buffer;
|
asio::streambuf content_buffer;
|
||||||
|
|
||||||
Response(): content(&content_buffer) {}
|
Response(): content(content_buffer) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Config {
|
class Config {
|
||||||
|
|
|
||||||
|
|
@ -184,7 +184,7 @@ int main() {
|
||||||
|
|
||||||
// synchronous request examples
|
// synchronous request examples
|
||||||
auto r1=client.request("GET", "/match/123");
|
auto r1=client.request("GET", "/match/123");
|
||||||
cout << r1->content.rdbuf() << endl;
|
cout << r1->content.rdbuf() << endl; // Alternatively, use the convenience function r1->content.string()
|
||||||
|
|
||||||
string json_string="{\"firstName\": \"John\",\"lastName\": \"Smith\",\"age\": 25}";
|
string json_string="{\"firstName\": \"John\",\"lastName\": \"Smith\",\"age\": 25}";
|
||||||
auto r2=client.request("POST", "/string", json_string);
|
auto r2=client.request("POST", "/string", json_string);
|
||||||
|
|
|
||||||
|
|
@ -181,7 +181,7 @@ int main() {
|
||||||
|
|
||||||
// synchronous request examples
|
// synchronous request examples
|
||||||
auto r1=client.request("GET", "/match/123");
|
auto r1=client.request("GET", "/match/123");
|
||||||
cout << r1->content.rdbuf() << endl;
|
cout << r1->content.rdbuf() << endl; // Alternatively, use the convenience function r1->content.string()
|
||||||
|
|
||||||
string json_string="{\"firstName\": \"John\",\"lastName\": \"Smith\",\"age\": 25}";
|
string json_string="{\"firstName\": \"John\",\"lastName\": \"Smith\",\"age\": 25}";
|
||||||
auto r2=client.request("POST", "/string", json_string);
|
auto r2=client.request("POST", "/string", json_string);
|
||||||
|
|
|
||||||
|
|
@ -126,6 +126,7 @@ namespace SimpleWeb {
|
||||||
size_t size() {
|
size_t size() {
|
||||||
return streambuf.size();
|
return streambuf.size();
|
||||||
}
|
}
|
||||||
|
/// Convenience function to return std::string. Note that the stream buffer is emptied when this functions is used.
|
||||||
std::string string() {
|
std::string string() {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << rdbuf();
|
ss << rdbuf();
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,13 @@ int main() {
|
||||||
assert(output.str()=="A string");
|
assert(output.str()=="A string");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
stringstream output;
|
||||||
|
auto r=client.request("POST", "/string", "A string");
|
||||||
|
assert(SimpleWeb::status_code(r->status_code)==SimpleWeb::StatusCode::success_ok);
|
||||||
|
assert(r->content.string()=="A string");
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
stringstream output;
|
stringstream output;
|
||||||
auto r=client.request("POST", "/string2", "A string");
|
auto r=client.request("POST", "/string2", "A string");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue