2

I recentely update my guzzle version from 3 to 6. The following call was working on 3 but now I need to upgrade it to 6 (as it is not working). After reading the docs, I am a little confused how this new post request works in Guzzle 6. Here is my OLD post request with Guzzle 3

 try
        {
            $request = $this->guzzleClient->post(
                '/login?token='.$this->container->getParameter("token"),
                array(),
                json_encode($data)
            );
            $request->setHeader('Content-Type', 'application/json');
            $response = $request->send();

            return $response->json();
        }

How do I transalate that so that it post the request?

  • Looks like this is what you're looking for: https://stackoverflow.com/questions/30860235/guzzle-ver-6-post-method-is-not-woking – Oluwatobi Samuel Omisakin Jan 22 '20 at 15:25
  • @OluwatobiSamuelOmisakin - Mark it as a duplicate if the answer on that question applies here as well. – M. Eriksson Jan 22 '20 at 15:28
  • 1
    Does this answer your question? [guzzle ver 6 post method is not woking](https://stackoverflow.com/questions/30860235/guzzle-ver-6-post-method-is-not-woking) – M. Eriksson Jan 22 '20 at 15:30
  • @MagnusEriksson not really. –  Jan 22 '20 at 15:57
  • Please give a little more feedback than "not really". If you rewrite your code as the code from the accepted answer, what happens? Error messages? Invalid request? – M. Eriksson Jan 22 '20 at 16:12
  • @MagnusEriksson sorry about that, nothing, I get no error back. the post call gets bypassed. –  Jan 22 '20 at 16:53
  • This PHP code shouldn't be here: `json_encode($data)` maybe you meant `JSON.parse(data)` – Triby Jan 22 '20 at 17:58

1 Answers1

2

You need this:

$response = $this->guzzle6->post(
    '/login?token='.$this->container->getParameter("token"),
    [
        'json' => $data
    ]
);

return json_decode($response->getBody()->getContents());

Guzzle 6 doesn't have ->json() for responses, so you have to decode it by yourself.

Alexey Shokov
  • 4,775
  • 1
  • 21
  • 22