ResponseBody.create(MediaType, String) constructs an object which can be mutated into an invalid state using ResponseBody.string()
See original GitHub issueBasically we bumped into this when writing unit tests to our component which uses Retrofit and therefore OkHttp. The version of OkHttp is 2.5.0.
When you construct an object using the ResponseBody.create(MediaType, String)
, two subsequent calls to ResponseBody.string()
will fail with an IOException
so the getter seems to mutate the object and mutates it to corrupted state. The behaviour is not indicated by the javadoc of ResponseBody.string()
and therefore it was a surprise and we did spend for a while wondering what’s the problem here. RealResponseBody
does not seem to be affected by the same issue.
To reproduce the problem:
@Test
public void testResponseBodyCreate() throws IOException {
ResponseBody body = ResponseBody.create(MediaType.parse("text/plain"), "test");
body.string();
body.string(); // Fails with IOException when using okhttp 2.5.0
}
Which reports
java.io.IOException: Content-Length and stream length disagree
at com.squareup.okhttp.ResponseBody.bytes(ResponseBody.java:62)
at com.squareup.okhttp.ResponseBody.string(ResponseBody.java:83)
This probably should be documented in the javadocs or the behaviour should be fixed.
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
java - Okhttp3 - RequestBody.create(contentType, content) ...
Java Solution: Use create(String, MediaType) instead of create(MediaType, String) for example. Kotlin Solution: Use the extension function ...
Read more >okhttp3.ResponseBody.create java code examples
Returns a new response body that transmits {@code content}. If {@code contentType} is non-null * and lacks a charset, this will use UTF-8....
Read more >Table of Contents - Micronaut Documentation
Micronaut aims to provide all the tools necessary to build JVM applications including: Dependency Injection and Inversion of Control (IoC). Aspect Oriented ...
Read more >oak@v11.1.0
A record of application state, which can be strongly typed by specifying a generic argument when constructing an Application() , or inferred by...
Read more >Using Spring ResponseEntity to Manipulate the HTTP ...
Consequently, we can use any type as the response body: @GetMapping("/hello") ResponseEntity<String> hello() { return new ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Every
ResponseBody
instance can only be read once, in practice, as there’s no automatic buffering happening.string()
is effectively an exhaustive read of the input so on a subsequent call the underlying data source is empty (hence the exception message).Application code should cache the response themselves or replace the RequestBody instance when consuming the stream.
On Wed, Nov 11, 2015, 3:18 AM mkatsoho notifications@github.com wrote: