Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http.server: handle an empty Json body #2042

Merged
merged 1 commit into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions http/vibe/http/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,7 @@ final class HTTPServerRequest : HTTPRequest {
if (icmp2(contentType, "application/json") == 0 || icmp2(contentType, "application/vnd.api+json") == 0 ) {
auto bodyStr = bodyReader.readAllUTF8();
if (!bodyStr.empty) _json = parseJson(bodyStr);
else _json = Json.undefined;
} else {
_json = Json.undefined;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/vibe.http.server.empty-json/dub.sdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name "tests"
description "Receive an empty JSON body"
dependency "vibe-d:http" path="../../"
34 changes: 34 additions & 0 deletions tests/vibe.http.server.empty-json/source/app.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import vibe.core.core : runTask, runEventLoop, exitEventLoop;
import vibe.data.json : serializeToJsonString;
import vibe.http.client : requestHTTP;
import vibe.http.server;
import vibe.stream.operations : readAllUTF8;

import std.conv : to;

void main()
{
auto s1 = new HTTPServerSettings;
s1.options &= ~HTTPServerOption.errorStackTraces;
s1.bindAddresses = ["127.0.0.1"];
s1.port = 11721;
listenHTTP(s1, &handler);

runTask({
scope (exit) exitEventLoop();
try {
auto req = requestHTTP("http://127.0.0.1:" ~ s1.port.to!string);
assert(req.bodyReader.readAllUTF8 == "JSON: null - World!\r\n");
} catch (Exception e) {
assert(false, e.msg);
}
});
runEventLoop();
}

void handler(scope HTTPServerRequest req, scope HTTPServerResponse res)
{
req.contentType = "application/json; charset=UTF-8";
res.bodyWriter.write("JSON: " ~ req.json.serializeToJsonString);
res.bodyWriter.write(" - World!\r\n");
}