Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 80ec134

Browse files
committed
POST with empty generates an error fix jooby-project#444
1 parent b7ba80b commit 80ec134

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.jooby.issues;
2+
3+
import java.net.URISyntaxException;
4+
import java.nio.ByteBuffer;
5+
6+
import org.jooby.test.ServerFeature;
7+
import org.junit.Test;
8+
9+
public class Issue444 extends ServerFeature {
10+
11+
{
12+
13+
parser((type, ctxt) -> {
14+
if (type.getRawType() == ByteBuffer.class) {
15+
return ctxt.body(body -> {
16+
return ByteBuffer.wrap(body.bytes());
17+
});
18+
}
19+
return ctxt.next();
20+
});
21+
22+
post("/444", req -> {
23+
ByteBuffer buffer = req.body().to(ByteBuffer.class);
24+
return buffer.remaining();
25+
});
26+
}
27+
28+
@Test
29+
public void shouldAcceptPostWithoutContentType() throws URISyntaxException, Exception {
30+
request()
31+
.post("/444")
32+
.body("abc", null)
33+
.expect("3");
34+
}
35+
}

jooby/src/main/java/org/jooby/internal/RequestImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ public List<Cookie> cookies() {
248248
public Mutant body() throws Exception {
249249
long length = length();
250250
if (length > 0) {
251-
if (MediaType.form.matches(type()) || MediaType.multipart.matches(type())) {
251+
MediaType type = type();
252+
if (!type.isAny() && (MediaType.form.matches(type) || MediaType.multipart.matches(type))) {
252253
return params();
253254
}
254255
File fbody = new File(

jooby/src/test/java/org/jooby/test/Client.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import static org.junit.Assert.assertEquals;
55
import static org.junit.Assert.assertNull;
66

7+
import java.io.ByteArrayInputStream;
78
import java.io.IOException;
89
import java.lang.reflect.Constructor;
910
import java.lang.reflect.InvocationTargetException;
1011
import java.net.URI;
12+
import java.nio.charset.StandardCharsets;
1113
import java.util.ArrayList;
1214
import java.util.List;
1315
import java.util.Optional;
@@ -27,6 +29,7 @@
2729
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
2830
import org.apache.http.conn.ssl.SSLContexts;
2931
import org.apache.http.entity.ContentType;
32+
import org.apache.http.entity.InputStreamEntity;
3033
import org.apache.http.entity.mime.MultipartEntityBuilder;
3134
import org.apache.http.impl.client.BasicCookieStore;
3235
import org.apache.http.impl.client.CloseableHttpClient;
@@ -111,7 +114,13 @@ public void close() {
111114
}
112115

113116
public Request body(final String body, final String type) {
114-
req.bodyString(body, ContentType.parse(type));
117+
if (type == null) {
118+
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
119+
HttpEntity entity = new InputStreamEntity(new ByteArrayInputStream(bytes), bytes.length);
120+
req.body(entity);
121+
} else {
122+
req.bodyString(body, ContentType.parse(type));
123+
}
115124
return this;
116125
}
117126

@@ -253,9 +262,9 @@ public Response header(final String headerName, final Optional<Object> headerVal
253262

254263
public Response header(final String headerName, final Callback callback) throws Exception {
255264
callback.execute(
256-
Optional.ofNullable(rsp.getFirstHeader(headerName))
257-
.map(Header::getValue)
258-
.orElse(null));
265+
Optional.ofNullable(rsp.getFirstHeader(headerName))
266+
.map(Header::getValue)
267+
.orElse(null));
259268
return this;
260269
}
261270

0 commit comments

Comments
 (0)