3

I'm trying to send a HTTP request to a server via boost asio. I have the following request that I have created in c++:

static const std::string TEMPLATE_HEADER =
"POST " + HTTP_PATH + " HTTP/1.1\r\n"
"Content-Type: application/json\r\n"
"Host: " + HOST_TAG + "\r\n"
"{ " + TEMPLATE_BODY + " }\r\n\r\n";

The actual request that i am sending on is this:

POST /eikon/authsession/machines/ HTTP/1.1 Content-Type: application/json Host: amers1.am.cp.icp2.mpp.ime.reuters.com:80 { "machineID" : "EP-03A482F1B88A","password" : "PasswordReset","takeESOControl" : "false","deviceID" : "123" }

This works when using Postman (a Google Chrome extension), but this doesn't seem to work using boost asio. I do the following (the above HTTP Request is strTemp.

// Write the request to the buffer
size_t request_length = strTemp.length();
boost::asio::write(s, boost::asio::buffer(strTemp, request_length));

char reply[max_length];

// When a reply is received ending in the correct tag we are looking for, store it in the response buffer
boost::asio::streambuf response;
boost::asio::read_until(s, response, Constants::BRACE_TAG);

Where the BRACE_TAG variable = "}" (as the response returned ends with a curly brace.

I get an exception come back that says "Exception: read_until: End of file".

Anyone have an idea what's happening?

Thanks.

sehe
  • 374,641
  • 47
  • 450
  • 633
Suzan Aydın
  • 355
  • 6
  • 17
  • 2
    This question is exactly the same as your question [posted 6 hours ago](http://stackoverflow.com/q/29380858/85371). It's bad etiquette to repost your questions to get attention. – sehe Apr 01 '15 at 06:44
  • I didn't get any answers for it in the space of 6 hours, yet posting it now I got an answer. It's deleted anyway - it would have been bad etiquette for me to have left it up and post again. – Suzan Aydın Apr 01 '15 at 06:53
  • 1
    Note it was ***me*** who answered. And I even _upvoted_. I don't know why you're so upset. Oh, and I was answering the "old" version of your question, but then you deleted it. – sehe Apr 01 '15 at 09:03

1 Answers1

2
  1. between the headers and the body there should be a double newline:

    "Host: " + HOST_TAG + "\r\n\r\n"
    
  2. You might be required to send a Content-Length header (What's the "Content-Length" field in HTTP header?). Postman might add it for you

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    @chmike True. EOF is just the socket being closed. – sehe Jul 24 '15 at 14:44
  • Did this answer OP's question? His code is just doing read_until from the beginning so I don't see why having double newline after headers or having content length in header would matter. – CodePro_NotYet Apr 12 '20 at 09:51
  • 1
    @CodePro_NotYet I think so. They specify is works in postman, but not using their code. If the server replies with an error the read until "}" will reach EOF, so the reported error is precisely to be expected. The hints explain the things that are demonstrably wrong in the code (it cannot work) as well as something that might additionally be different when using Postman. Of course it would be more helpful if we ever got a reaction from the OP. – sehe Apr 12 '20 at 14:36
  • 1
    Got it. Also, your comment made me realise I had assumed OP was a "he". Thanks :) – CodePro_NotYet Apr 13 '20 at 13:18