RejectedSoftware Forums

Sign up

Invalid HTTP format when using requestHTTP

Hi all,

This may well be something I'm doing incorrectly (in fact I hope it is). I am trying to send a HTTP POST request to a local server (running Ruby on Rails under puma.) The code below is almost verbatim from the online sample:

auto response = requestHTTP("http://localhost:3000/users/login",
		(scope req) {
			req.method = HTTPMethod.POST;
			req.headers["Accept"] = "application/json";
			auto data = ["user_email": username, "user_password": password];
			req.writeJsonBody(data);
		}
	);

However, although it hits the correct endpoint/route on the Rails server, the data I am posting is not making it through. I have posted the full output of the Rails server below.

Started POST "/users/login" for 127.0.0.1 at 2014-08-14 15:45:54 +1000
Processing by SessionsController#create as JSON
  Parameters: {"session"=>{}}
Completed 401 Unauthorized in 1ms
2014-08-14 15:45:54 +1000: HTTP parse error, malformed request (): #<Puma::HttpParserError: Invalid HTTP format, parsing fails.>
2014-08-14 15:45:54 +1000: ENV: {"rack.version"=>[1, 2], "rack.errors"=>#<IO:<STDERR>>, "rack.multithread"=>true, "rack.multiprocess"=>false, "rack.run_once"=>false, "SCRIPT_NAME"=>"", "CONTENT_TYPE"=>"text/plain", "QUERY_STRING"=>"", "SERVER_PROTOCOL"=>"HTTP/1.1", "SERVER_SOFTWARE"=>"2.8.2", "GATEWAY_INTERFACE"=>"CGI/1.2"}

If anybody could help me out with this one, albeit with the limited debug info, I'd be eternally grateful.

Regards,

David.

Re: Invalid HTTP format when using requestHTTP

Interestingly enough, this code DOES work:

    import std.net.curl;

	auto data = serializeToJson(["user": ["email": username, "password": password]]).toString();

	auto client = HTTP("localhost:3000/users/login");
	client.postData = data;
	client.addRequestHeader("Accept", "application/json");
	client.addRequestHeader("Content-Type", "application/json");
	client.perform();
	auto status = client.statusLine;
	logInfo("Status: %s", status);
	if (status.code == 201) return true;
	return false;

I'm not sure what the vibe libraries are doing differently or if it really is a bug?

Re: Invalid HTTP format when using requestHTTP

Can you try to capture the RAW TCP network traffic with Wireshark or
tcpdump (or similar)? That would help a lot to figure out what went
wrong. The vibe.d code snippet doesn't look suspicious from the outside.

Re: Invalid HTTP format when using requestHTTP

On Fri, 15 Aug 2014 04:56:26 GMT, David Monagle wrote:

I'm not sure what the vibe libraries are doing differently or if it really is a bug?

There's currently a bug where a bad keep-alive condition causes the connection to close prematurely. You can try this pull request: https://github.com/rejectedsoftware/vibe.d/pull/756