Giter Club home page Giter Club logo

Comments (4)

kenorb avatar kenorb commented on May 1, 2024 2

The $response->getBody() should be enough, however when calling it for the first time, you've to rewind it if you're planning to access content for the second time.

For example, I have called $response->getBody(), then when calling getContents();, the result was always empty. Doing $response->rewind(); in this case helped.

Related: Guzzlehttp - How get the body of a response from Guzzle 6?

$contents = (string) $response->getBody();

vs

$contents = $response->getBody()->getContents();

The difference between the two approaches is that getContents returns the remaining contents, so that a second call returns nothing unless you seek the position of the stream with rewind or seek .

from psr7.

mtdowling avatar mtdowling commented on May 1, 2024

If you need to seek and store the entire stream in memory then cast the stream to a string using (string)

On Sep 14, 2015, at 8:49 AM, Veaceslav Medvedev [email protected] wrote:

Hello, I hit one problem when use instance of GuzzleHttp\Psr7\Stream.

echo $response->getBody()->getContents();
method returns me empty string, although the logger show me correct response.

The problem was that the logger use (string) $response->getBody() and we seek to the end of stream.

Should we seek back before returns content?

public function getContents()
{
$this->rewind();
$contents = stream_get_contents($this->stream);

if ($contents === false) {
    throw new \RuntimeException('Unable to read stream contents');
}

return $contents;

}
Seems __toString() can call getContent().

β€”
Reply to this email directly or view it on GitHub.

from psr7.

slavcodev avatar slavcodev commented on May 1, 2024

Well, I think getContents meaning all body contents, not a part. To read body partial we have read($length). Or I''m wrong?

from psr7.

mtdowling avatar mtdowling commented on May 1, 2024

See https://github.com/php-fig/http-message/blob/master/src/StreamInterface.php#L143

from psr7.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.