No JSON response body
sd205 opened this issue · 13 comments
I saw my issue #427 was closed. In order to test the fix, I've cloned your repository and built the 2.1.0.BETA JARs. (For which I had to disable some unit tests that were failing: DateKitTest, HttpRequestTest, ANYTest) I then refactored my code, since there is zero backwards compatibility (annoying!)
I am able to confirm that the original issue is fixed - I can pass in a JSON body with a value contain an equals sign, e.g. {"str":"="}
However, now I have a worse problem - there is no body at all on the response! It just returns empty with 200. I have read the README and updated my annotations, e.g.
@post(value = "/api/parse", responseType = ResponseType.JSON)
You can find my simple test case in the original issue. You'll obviously have to refactor...
@sd205 can you send your request by curl?
Interesting... using curl I have both issues!
When passing equals sign:
curl -v http://localhost:9000/api/parse -d {"formatString":"=","delimiters":null,"params":["","","","","","","","","",""]}
- Trying 127.0.0.1:9000...
- Connected to localhost (127.0.0.1) port 9000 (#0)
POST /api/parse HTTP/1.1
Host: localhost:9000
User-Agent: curl/7.79.1
Accept: /
Content-Length: 51
Content-Type: application/x-www-form-urlencoded
- Mark bundle as not supporting multiuse
< HTTP/1.1 500 Internal Server Error
< Content-Length: 3701
< Date: Wed, 04 May 2022 14:33:30 GMT
< X-Powered-By: blade-2.1.0.BETA
< Content-Type: text/html; charset=UTF-8
< Connection: keep-alive
<
color: #fff;
font-weight: bold;-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #fff;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
line-height: 1.25;
padding: 0.5em 0.75em;
position: relative;}.message-body{background-color: #fff5f7;font-size:1rem;border-color: #ff3860;
color: #900C3F;
border-top-left-radius: 0;
border-top-right-radius: 0;
border-top: none;
border: 1px solid #dbdbdb;
border-radius: 3px;
padding: 1em 1.25em;}</style>
500 Internal Server Error
Request URI: /api/parse
Error Message: com.google.gson.stream.MalformedJsonException: Expected value at li
Copyright © Blade-2.1.0.BETA
* Connection #0 to host localhost left intact $.formatString
When passing some other string:
curl -v http://localhost:9000/api/parse -d {"formatString":"a","delimiters":null,"params":["","","","","","","","","",""]}
- Trying 127.0.0.1:9000...
- Connected to localhost (127.0.0.1) port 9000 (#0)
POST /api/parse HTTP/1.1
Host: localhost:9000
User-Agent: curl/7.79.1
Accept: /
Content-Length: 51
Content-Type: application/x-www-form-urlencoded
- Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Length: 0
< Date: Wed, 04 May 2022 14:33:50 GMT
< X-Powered-By: blade-2.1.0.BETA
< Connection: keep-alive
< - Connection #0 to host localhost left intact
@sd205 try this code
@POST("/read_body")
public String readBody(@Body Map<String, Object> body) {
log.info("body = {}", body);
return body.toString();
}
@sd205 you are requesting JSON
data, and the request contentType
should be application/json
.
Added Content-Type and Accept headers, which yielded no improvement:
curl -v http://localhost:9000/api/parse -H "Content-Type: application/json" -H "Accept: application/json" -d {"formatString":"s","delimiters":null,"params":["","","","","","","","","",""]}
- Trying 127.0.0.1:9000...
- Connected to localhost (127.0.0.1) port 9000 (#0)
POST /api/parse HTTP/1.1
Host: localhost:9000
User-Agent: curl/7.79.1
> Content-Type: application/json
Accept: application/json
Content-Length: 51
- Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Length: 0
< Date: Wed, 04 May 2022 16:11:42 GMT
< X-Powered-By: blade-2.1.0.BETA
< Connection: keep-alive
< - Connection #0 to host localhost left intact
curl -v http://localhost:9000/api/parse -H "Content-Type: application/json" -H "Accept: application/json" -d {"formatString":"=","delimiters":null,"params":["","","","","","","","","",""]}
- Trying 127.0.0.1:9000...
- Connected to localhost (127.0.0.1) port 9000 (#0)
POST /api/parse HTTP/1.1
Host: localhost:9000
User-Agent: curl/7.79.1
> Content-Type: application/json
Accept: application/json
Content-Length: 51
- Mark bundle as not supporting multiuse
< HTTP/1.1 500 Internal Server Error
< Content-Length: 3701
< Date: Wed, 04 May 2022 16:11:59 GMT
< X-Powered-By: blade-2.1.0.BETA
< Content-Type: text/html; charset=UTF-8
< Connection: keep-alive
<
color: #fff;
font-weight: bold;-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #fff;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
line-height: 1.25;
padding: 0.5em 0.75em;
position: relative;}.message-body{background-color: #fff5f7;font-size:1rem;border-color: #ff3860;
color: #900C3F;
border-top-left-radius: 0;
border-top-right-radius: 0;
border-top: none;
border: 1px solid #dbdbdb;
border-radius: 3px;
padding: 1em 1.25em;}</style>
500 Internal Server Error
Request URI: /api/parse
Error Message: com.google.gson.stream.MalformedJsonException: Expected value at li
Copyright © Blade-2.1.0.BETA
* Connection #0 to host localhost left intact $.formatString
@sd205 try this code
@POST("/read_body") public String readBody(@Body Map<String, Object> body) { log.info("body = {}", body); return body.toString(); }
I'm trying to use the documented "Body Parameter to Model" and "RenderResponse" "By Annotation" features. When it doesn't trip over the equals sign, it parses the model fine and invokes my method - I can see the logging of inputs and outputs from the method. To reiterate, my issues are:
- Input body-to-model transformation not working only when the JSON value contains an equals sign - as per #427
- Output model-to-body transformation not working at all - new issue in 2.1.0.BETA
@sd205 can you upload your code snippet to Github and I'll take a look?
Test case attached. Server log in the ZIP - you can see that it reaches the Router method ok for {"str":"s"} but not for {"str":"="}, and that it returns the same model object as the response. Curl logs below - Content-Length=0 for {"str":"s"}:
curl -v http://localhost:8999/test -H "Content-Type: application/json" -H "Accept: application/json" -d {"str":"s"}
- Trying 127.0.0.1:8999...
- Connected to localhost (127.0.0.1) port 8999 (#0)
POST /test HTTP/1.1
Host: localhost:8999
User-Agent: curl/7.79.1
Content-Type: application/json
Accept: application/json
Content-Length: 7
- Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Length: 0
< Date: Thu, 05 May 2022 09:54:58 GMT
< X-Powered-By: blade-2.1.0.BETA
< Connection: keep-alive
< - Connection #0 to host localhost left intact
curl -v http://localhost:8999/test -H "Content-Type: application/json" -H "Accept: application/json" -d {"str":"="}
- Trying 127.0.0.1:8999...
- Connected to localhost (127.0.0.1) port 8999 (#0)
POST /test HTTP/1.1
Host: localhost:8999
User-Agent: curl/7.79.1
Content-Type: application/json
Accept: application/json
Content-Length: 7
- Mark bundle as not supporting multiuse
< HTTP/1.1 500 Internal Server Error
< Content-Length: 3666
< Date: Thu, 05 May 2022 09:55:06 GMT
< X-Powered-By: blade-2.1.0.BETA
< Content-Type: text/html; charset=UTF-8
< Connection: keep-alive
<
color: #fff;
font-weight: bold;-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #fff;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
line-height: 1.25;
padding: 0.5em 0.75em;
position: relative;}.message-body{background-color: #fff5f7;font-size:1rem;border-color: #ff3860;
color: #900C3F;
border-top-left-radius: 0;
border-top-right-radius: 0;
border-top: none;
border: 1px solid #dbdbdb;
border-radius: 3px;
padding: 1em 1.25em;}</style>
500 Internal Server Error
Request URI: /test
Error Message: com.google.gson.stream.MalformedJsonException: Expected value at line 1
Copyright © Blade-2.1.0.BETA
* Connection #0 to host localhost left intact
Taken 2.1.1.BETA now.
Again I had unit test failures:
testQueryParam(com.hellokaton.blade.mvc.HttpRequestTest) Time elapsed: 0.003 sec <<< ERROR!
java.util.NoSuchElementException: No value present
at java.util.Optional.get(Optional.java:135)
at com.hellokaton.blade.mvc.HttpRequestTest.testQueryParam(HttpRequestTest.java:184)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
I still get the same two issues:
curl -v http://localhost:8999/test -H "Content-Type: application/json" -H "Accept: application/json" -d {"str":"s"}
- Trying 127.0.0.1:8999...
- Connected to localhost (127.0.0.1) port 8999 (#0)
POST /test HTTP/1.1
Host: localhost:8999
User-Agent: curl/7.79.1
Content-Type: application/json
Accept: application/json
Content-Length: 7
- Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Length: 0
< Date: Thu, 05 May 2022 15:25:03 GMT
< X-Powered-By: blade-2.1.1.BETA
< Connection: keep-alive
< - Connection #0 to host localhost left intact
curl -v http://localhost:8999/test -H "Content-Type: application/json" -H "Accept: application/json" -d {"str":"="}
- Trying 127.0.0.1:8999...
- Connected to localhost (127.0.0.1) port 8999 (#0)
POST /test HTTP/1.1
Host: localhost:8999
User-Agent: curl/7.79.1
Content-Type: application/json
Accept: application/json
Content-Length: 7
- Mark bundle as not supporting multiuse
< HTTP/1.1 500 Internal Server Error
< Content-Length: 3666
< Date: Thu, 05 May 2022 15:25:13 GMT
< X-Powered-By: blade-2.1.1.BETA
< Content-Type: text/html; charset=UTF-8
< Connection: keep-alive
<
color: #fff;
font-weight: bold;-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
color: #fff;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
line-height: 1.25;
padding: 0.5em 0.75em;
position: relative;}.message-body{background-color: #fff5f7;font-size:1rem;border-color: #ff3860;
color: #900C3F;
border-top-left-radius: 0;
border-top-right-radius: 0;
border-top: none;
border: 1px solid #dbdbdb;
border-radius: 3px;
padding: 1em 1.25em;}</style>
500 Internal Server Error
Request URI: /test
Error Message: com.google.gson.stream.MalformedJsonException: Expected value at line 1
Copyright © Blade-2.1.1.BETA
* Connection #0 to host localhost left intact
Hi - done some more testing this morning, prompted by your postman test yesterday, and found that CURL with data from file works ok with equals sign:
curl -v http://localhost:8999/test -H "Content-Type: application/json" -H "Accept: application/json" -d "@test.json"
- Trying 127.0.0.1:8999...
- Connected to localhost (127.0.0.1) port 8999 (#0)
POST /test HTTP/1.1
Host: localhost:8999
User-Agent: curl/7.79.1
Content-Type: application/json
Accept: application/json
Content-Length: 12
- Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Length: 0
< Date: Fri, 06 May 2022 08:54:50 GMT
< X-Powered-By: blade-2.1.1.BETA
< Connection: keep-alive
< - Connection #0 to host localhost left intact
So the first issue appears fixed, in both 2.1.0.BETA and 2.1.1.BETA, which is progress!
However, I'm still getting no response body - as you see above, Content-Length: 0 for the response
< Content-Length: 0
This problem does exist and has been fixed in the just released 2.1.1.RELEASE
Confirmed - all fixed in 2.1.1.RELEASE
Thanks for your help!!