RejectedSoftware Forums

Sign up

requestHTTP with body

Hi guys, tearing my hair out with this one. I'm trying to do a HTTP POST request with a JSON body. Seems straight forward and there is even an example in the Vibe docs. However the body never seems to make it through to the server. In order to make sure I wasn't doing something stupid (always a possibility) I modified the http_request example like so:

import vibe.core.log;
import vibe.http.client;

void main()
{
	requestHTTP("http://requestb.in/ny54b8ny",
		(scope req) {
			req.method = HTTPMethod.POST;
			req.writeJsonBody(["name": "My Name"]);
		},
		(scope res) {
			logInfo("Response: %d", res.statusCode);
			foreach (k, v; res.headers)
				logInfo("Header: %s: %s", k, v);
		}
	);
}

Checking the requestbin the json headers are set correctly but the RAW body is empty. Just as I am experiencing with my main project. I may just be missing something, can anybody help out please?

Re: requestHTTP with body

On Mon, 22 Sep 2014 12:27:04 GMT, David Monagle wrote:

Hi guys, tearing my hair out with this one. I'm trying to do a HTTP POST request with a JSON body. Seems straight forward and there is even an example in the Vibe docs. However the body never seems to make it through to the server. In order to make sure I wasn't doing something stupid (always a possibility) I modified the http_request example like so:

(...)

Checking the requestbin the json headers are set correctly but the RAW body is empty. Just as I am experiencing with my main project. I may just be missing something, can anybody help out please?

It sounds like the issue is on the server side. By default, the HTTPServerOption.parseJsonBody option is set in HTTPServerSettings. This causes any JSON body in a request to be read (consumed) and parsed into req.json, and req.bodyReader stream will then be empty. The solution is to clear this option:

auto settings = new HTTPServerSettings;
settings.options &= ~HTTPServerOption.parseJsonBody;
listenHTTP(settings, ...);

Re: requestHTTP with body

On Mon, 22 Sep 2014 12:40:34 GMT, Sönke Ludwig wrote:

On Mon, 22 Sep 2014 12:27:04 GMT, David Monagle wrote:

Hi guys, tearing my hair out with this one. I'm trying to do a HTTP POST request with a JSON body. Seems straight forward and there is even an example in the Vibe docs. However the body never seems to make it through to the server. In order to make sure I wasn't doing something stupid (always a possibility) I modified the http_request example like so:

(...)

Checking the requestbin the json headers are set correctly but the RAW body is empty. Just as I am experiencing with my main project. I may just be missing something, can anybody help out please?

It sounds like the issue is on the server side. By default, the HTTPServerOption.parseJsonBody option is set in HTTPServerSettings. This causes any JSON body in a request to be read (consumed) and parsed into req.json, and req.bodyReader stream will then be empty. The solution is to clear this option:

auto settings = new HTTPServerSettings;
settings.options &= ~HTTPServerOption.parseJsonBody;
listenHTTP(settings, ...);

Hi Sönke, thanks for the quick reply

The target server is not a vibe server so this is not a problem in my case.

My production server is a web service and in the example above I've used a service called request bin, http://requestb.in/ a very handy utility that will take any HTTP request and give you all the details on it. Perfect for testing this kind of circumstance. In both cases there was no request body at all.

Re: requestHTTP with body

On Mon, 22 Sep 2014 13:21:44 GMT, David Monagle wrote:

On Mon, 22 Sep 2014 12:40:34 GMT, Sönke Ludwig wrote:

On Mon, 22 Sep 2014 12:27:04 GMT, David Monagle wrote:

Hi guys, tearing my hair out with this one. I'm trying to do a HTTP POST request with a JSON body. Seems straight forward and there is even an example in the Vibe docs. However the body never seems to make it through to the server. In order to make sure I wasn't doing something stupid (always a possibility) I modified the http_request example like so:

(...)

Checking the requestbin the json headers are set correctly but the RAW body is empty. Just as I am experiencing with my main project. I may just be missing something, can anybody help out please?

It sounds like the issue is on the server side. By default, the HTTPServerOption.parseJsonBody option is set in HTTPServerSettings. This causes any JSON body in a request to be read (consumed) and parsed into req.json, and req.bodyReader stream will then be empty. The solution is to clear this option:

auto settings = new HTTPServerSettings;
settings.options &= ~HTTPServerOption.parseJsonBody;
listenHTTP(settings, ...);

Hi Sönke, thanks for the quick reply

The target server is not a vibe server so this is not a problem in my case.

My production server is a web service and in the example above I've used a service called request bin, http://requestb.in/ a very handy utility that will take any HTTP request and give you all the details on it. Perfect for testing this kind of circumstance. In both cases there was no request body at all.

Okay, I see. When looking at what gets written, everything looks correct, though:

POST /1a6yie41 HTTP/1.1
User-Agent: vibe.d/0.7.20 (HTTPClient, +http://vibed.org/)
Connection: keep-alive
Accept-Encoding: gzip, deflate
Host: requestb.in
Transfer-Encoding: chunked
Content-Type: application/json

12
{"name":"My Name"}
0

I'll look at it using Wireshark later to be sure, but maybe requestbin simply doesn't like chunked bodies?

There is code in HTTPServerResponse.writeJsonBody that by default tries to send JSON bodies with "Content-Length" instead of using chunked encoding. It would make sense to do the same in HTTPClientRequest.writeJsonBody, especially if that would fix the issue here.

Re: requestHTTP with body

Non-chunked transfer is now enabled by default. With this change, the body is displayed correctly for requestbin. Commit: fc00b8b

Re: requestHTTP with body

On Mon, 22 Sep 2014 14:42:46 GMT, Sönke Ludwig wrote:

Non-chunked transfer is now enabled by default. With this change, the body is displayed correctly for requestbin. Commit: fc00b8b

Thanks Sönke, I will try it out!