Thread: Converting From Boost::Asio::streambuf to a string

  1. #1
    Registered User
    Join Date
    Jan 2014

    Converting From Boost::Asio::streambuf to a string

    Hello, Can someone please tell me how to cast the &response variable below into a std::string? I have tried everything I know to do but the string is always empty.

    // sync_client.cpp
    // ~~~~~~~~~~~~~~~
    // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
    // Distributed under the Boost Software License, Version 1.0. (See accompanying
    // file LICENSE_1_0.txt or copy at
    #include <iostream>
    #include <istream>
    #include <ostream>
    #include <string>
    #include <boost/asio.hpp>
    using boost::asio::ip::tcp;
    int main(int argc, char* argv[])
        if (argc != 3)
          std::cout << "Usage: sync_client <server> <path>\n";
          std::cout << "Example:\n";
          std::cout << "  sync_client Boost C++ Libraries /LICENSE_1_0.txt\n";
          return 1;
        boost::asio::io_service io_service;
        // Get a list of endpoints corresponding to the server name.
        tcp::resolver resolver(io_service);
        tcp::resolver::query query(argv[1], "http");
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
        // Try each endpoint until we successfully establish a connection.
        tcp::socket socket(io_service);
        boost::asio::connect(socket, endpoint_iterator);
        // Form the request. We specify the "Connection: close" header so that the
        // server will close the socket after transmitting the response. This will
        // allow us to treat all data up until the EOF as the content.
        boost::asio::streambuf request;
        std::ostream request_stream(&request);
        request_stream << "GET " << argv[2] << " HTTP/1.0\r\n";
        request_stream << "Host: " << argv[1] << "\r\n";
        request_stream << "Accept: */*\r\n";
        request_stream << "Connection: close\r\n\r\n";
        // Send the request.
        boost::asio::write(socket, request);
        // Read the response status line. The response streambuf will automatically
        // grow to accommodate the entire line. The growth may be limited by passing
        // a maximum size to the streambuf constructor.
        boost::asio::streambuf response;
        boost::asio::read_until(socket, response, "\r\n");
        // Check that response is OK.
        std::istream response_stream(&response);
        std::string http_version;
        response_stream >> http_version;
        unsigned int status_code;
        response_stream >> status_code;
        std::string status_message;
        std::getline(response_stream, status_message);
        if (!response_stream || http_version.substr(0, 5) != "HTTP/")
          std::cout << "Invalid response\n";
          return 1;
        if (status_code != 200)
          std::cout << "Response returned with status code " << status_code << "\n";
          return 1;
        // Read the response headers, which are terminated by a blank line.
        boost::asio::read_until(socket, response, "\r\n\r\n");
        // Process the response headers.
        std::string header;
        while (std::getline(response_stream, header) && header != "\r")
          std::cout << header << "\n";
        std::cout << "\n";
        // Write whatever content we already have to output.
        if (response.size() > 0)
          std::cout << &response;
        // Read until EOF, writing data to output as we go.
        boost::system::error_code error;
        while (boost::asio::read(socket, response,
              boost::asio::transfer_at_least(1), error)) {
          std::cout << &response;
    //         s below is always empty        //
    std::ostringstream ss;
    ss <<response;
    std::string s = ss.str(); 
        if (error != boost::asio::error::eof)
          throw boost::system::system_error(error);
      catch (std::exception& e)
        std::cout << "Exception: " << e.what() << "\n";
      return 0;
    Last edited by EverydayDiesel; 01-05-2016 at 09:42 PM.

  2. #2
    Registered User
    Join Date
    Jan 2014
    essentially I am just trying to retrieve a csv file from a webserver and store it in a string (so I can later parse it)

    Can anyone please help with doing this?

  3. #3
    Registered User
    Join Date
    Oct 2006
    You can dump the contents of your streambuf to a std::stringstream. You can then convert that stringstream to a plain string.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-14-2013, 12:25 PM
  2. Boost Asio and asynchronous I/O
    By Elysia in forum C++ Programming
    Replies: 9
    Last Post: 06-19-2011, 07:51 PM
  3. Open a serial port asynchronously with boost asio?
    By TriKri in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2010, 12:33 PM
  4. Replies: 1
    Last Post: 05-14-2010, 03:07 AM
  5. Boost ASIO
    By PetrolMan in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2009, 03:24 PM