question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ResponseBody.create(MediaType, String) constructs an object which can be mutated into an invalid state using ResponseBody.string()

See original GitHub issue

Basically 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:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
JakeWhartoncommented, Nov 4, 2015

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).

0reactions
JakeWhartoncommented, Nov 11, 2015

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:

Just a suggestion. Can refer to the similar function in rest-assured, it is idempotent. I guess it cached the http resp body, and return it in later calls.

— Reply to this email directly or view it on GitHub https://github.com/square/okhttp/issues/1968#issuecomment-155701090.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found