From d1828e3fa47650b3d2160368b5e6c155203fff59 Mon Sep 17 00:00:00 2001 From: Steve Hu Date: Tue, 25 Aug 2020 13:57:44 -0400 Subject: [PATCH] fixes #753 handle text/plain and content type missing in BodyHandler (#755) --- .../java/com/networknt/body/BodyHandler.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/body/src/main/java/com/networknt/body/BodyHandler.java b/body/src/main/java/com/networknt/body/BodyHandler.java index 1ff0497409..4f99fad4e7 100644 --- a/body/src/main/java/com/networknt/body/BodyHandler.java +++ b/body/src/main/java/com/networknt/body/BodyHandler.java @@ -87,12 +87,12 @@ public BodyHandler() { public void handleRequest(final HttpServerExchange exchange) throws Exception { // parse the body to map or list if content type is application/json String contentType = exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE); + if (exchange.isInIoThread()) { + exchange.dispatch(this); + return; + } + exchange.startBlocking(); if (contentType != null) { - if (exchange.isInIoThread()) { - exchange.dispatch(this); - return; - } - exchange.startBlocking(); try { if (contentType.startsWith("application/json")) { InputStream inputStream = exchange.getInputStream(); @@ -103,6 +103,10 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception { } // attach the parsed request body into exchange if the body parser is enabled attachJsonBody(exchange, unparsedRequestBody); + } else if (contentType.startsWith("text/plain")) { + InputStream inputStream = exchange.getInputStream(); + String unparsedRequestBody = StringUtils.inputStreamToString(inputStream, StandardCharsets.UTF_8); + exchange.putAttachment(REQUEST_BODY, unparsedRequestBody); } else if (contentType.startsWith("multipart/form-data") || contentType.startsWith("application/x-www-form-urlencoded")) { // attach the parsed request body into exchange if the body parser is enabled attachFormDataBody(exchange); @@ -115,6 +119,10 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception { setExchangeStatus(exchange, CONTENT_TYPE_MISMATCH, contentType); return; } + } else { + // attach the stream to the exchange if the content type is missing. + InputStream inputStream = exchange.getInputStream(); + exchange.putAttachment(REQUEST_BODY, inputStream); } Handler.next(exchange, next); }