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.

How to read response's body without copying buffer

See original GitHub issue

Hey guys

I had to do something like response.body().bytes() in an Interceptor, then had to recreate a full Response with the fetched byte to return a clean Request that can be parsed again, instead of just switching the Reader Index back to it’s beginning. That’s a lot of temporary objects to garbage-collect.

Do you have a better solution?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
marcottedancommented, Sep 19, 2016

So basically, it would look like this?

final BufferedSource source = response.body().source();
source.request(Integer.MAX_VALUE);
final byte[] bytes = source.buffer().snapshot().toByteArray();

Will this solution also work with multipart requests?

4reactions
marcottedancommented, Sep 19, 2016

I’m using Jackson’s ObjectMapper readValue that can accepts both a byte[] or a String.

Should I simply call snapshot().utf8() ?

internalArray() is package-protected

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Read a http Response body to buffer in golang(without ...
You can use io.Copy which takes io.Writer and io.Reader . io.Copy uses 32KB buffer to copy from reader to writer until it reaches...
Read more >
Re-reading ASP.Net Core request bodies with EnableBuffering()
A straightforward solution is to save a copy of the stream in another stream that supports seeking so the content can be read...
Read more >
Should response Body be fully read for HTTP connection re ...
I think it does get read implicitly if it fits entirely in the read buffer, but there's no guarantee.
Read more >
Response.arrayBuffer() - Web APIs - MDN Web Docs
When the fetch is successful, we read an ArrayBuffer out of the response using arrayBuffer() , decode the audio data using AudioContext.
Read more >
How to convert an HTTP response body to a string in Go
ReadAll() in Go 1.15 and earlier) to read the whole body into a slice ... or just the response headers (without the body),...
Read more >

github_iconTop Related Medium Post

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