6

I need to write a command line client for playing tic-tac-toe over a server. the server accepts http requests and sends back json to my client. i am looking for a quick way to send a http request and receive the json as a string using boost libraries.

example http request = "http://???/newGame?name=david"
example json response = "\"status\":\"okay\", \"id\":\"game-23\", \"letter\":2"
Mladen Kajic
  • 125
  • 1
  • 2
  • 7
  • Note for serious business (mainly existing webservers that may do any manner of chunked encoding, compressions, keep-alive, redirect responses etc.) you will want to use a library like http://curl.haxx.se/libcurl/ – sehe Nov 05 '14 at 16:33

1 Answers1

10

The simplest thing that fits the description:

Live On Coliru

#include <boost/asio.hpp>
#include <iostream>

int main() {
    boost::system::error_code ec;
    using namespace boost::asio;

    // what we need
    io_service svc;
    ip::tcp::socket sock(svc);
    sock.connect({ {}, 8087 }); // http://localhost:8087 for testing

    // send request
    std::string request("GET /newGame?name=david HTTP/1.1\r\n\r\n");
    sock.send(buffer(request));

    // read response
    std::string response;

    do {
        char buf[1024];
        size_t bytes_transferred = sock.receive(buffer(buf), {}, ec);
        if (!ec) response.append(buf, buf + bytes_transferred);
    } while (!ec);

    // print and exit
    std::cout << "Response received: '" << response << "'\n";
}

This receives the full response. You can test it with a dummy server:
(also Live On Coliru):

netcat -l localhost 8087 <<< '"status":"okay", "id":"game-23", "letter":2'

This will show that the request is received, and the response will be written out by our client code above.

Note that for more ideas you could look at the examples http://www.boost.org/doc/libs/release/doc/html/boost_asio/examples.html (although they focus on asynchronous communications, because that's the topic of the Asio library)

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks alot for the reply :) but i am having a little trouble, with these lines :`code`sock.connect({ {}, 8087 })`code` and:`code`size_t bytes_transferred = sock.receive(buffer(buf), {}, ec)`code` What should i put inside the braces. i did a little reading and for the second one i tried making a null socket_base::message_flags flags; and passing that in but my program seems to crash. – Mladen Kajic Nov 07 '14 at 11:37
  • It's an `{{},8087}` is short for `ip::tcp::endpoint(ip::address(), 8087)`. What you put there is up to you (what do you want to connect to?). Usually, endusers specify address and port as strings, and you use **[`ip::tcp::resolver`](http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/reference/ip__tcp/resolver.html)** to resolve to the endpoint(s). Almost all the client samples will show you how to do this ((a)synchronously). Alternatively, `ip::address_v4::from_string("192.168.0.1")` can be used to hardcode a ipv4 address, e.g. – sehe Nov 07 '14 at 11:42
  • Oops. I misread, yes those flags are documented http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/reference/socket_base.html#boost_asio.reference.socket_base.data_members – sehe Nov 07 '14 at 11:49
  • How do you send request in secure format that is on https ? @sehe – CocoCrisp Oct 13 '16 at 13:13
  • You just connect over ssl. @Milind http://coliru.stacked-crooked.com/a/9546326fd1def416 – sehe Oct 14 '16 at 12:02
  • There are couple of build errors in this 1.'boost::asio::ssl::context::method' is not a class or namespace. 2.'boost::asio::ssl::stream_base::handshake_type' is not a class or namespace. @sehe – CocoCrisp Oct 17 '16 at 06:50
  • You should post a question describing your source and build setup. My sample does not have the build errors, as you can see **[Live on Coliru](http://coliru.stacked-crooked.com/a/9546326fd1def416)** – sehe Oct 17 '16 at 07:28
  • Ok, this is the simplest hard-coded HTTP request, but do you expect me to write the 200pages full HTTP spec? – Adrian Maire Mar 19 '18 at 19:54
  • @AdrianMaire I don't recall you asking me a question. What makes you think any instructions applied to you? Besides, nobody recommends to "write the spec" [sic]. In fact, before I answered [I noted exactly that](https://stackoverflow.com/questions/26761058/how-to-send-http-request-and-retrieve-a-json-response-c-boost/26761889?noredirect=1#comment42105808_26761058). These days there are other libraries. They're not hard to find (e.g. https://stackoverflow.com/q/49353601/85371) – sehe Mar 19 '18 at 20:25