Added convenience function Client::Response::Content::string()

This commit is contained in:
eidheim 2017-06-22 22:00:07 +02:00
commit 6b7fd993cf
5 changed files with 29 additions and 4 deletions

View file

@ -37,20 +37,37 @@ namespace SimpleWeb {
template <class socket_type>
class ClientBase {
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 {
friend class ClientBase<socket_type>;
friend class Client<socket_type>;
public:
std::string http_version, status_code;
std::istream content;
Content content;
CaseInsensitiveMultimap header;
private:
asio::streambuf content_buffer;
Response(): content(&content_buffer) {}
Response(): content(content_buffer) {}
};
class Config {

View file

@ -184,7 +184,7 @@ int main() {
// synchronous request examples
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}";
auto r2=client.request("POST", "/string", json_string);

View file

@ -181,7 +181,7 @@ int main() {
// synchronous request examples
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}";
auto r2=client.request("POST", "/string", json_string);

View file

@ -126,6 +126,7 @@ namespace SimpleWeb {
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();

View file

@ -70,6 +70,13 @@ int main() {
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;
auto r=client.request("POST", "/string2", "A string");