guzzlehttp and symfony with rest api problem












0















I want to make a post call with rest api using guzzlehttp in symfony ...
I wrote this code but the response



/**
* @Route("/post/")
*/
public function postAction()
{
$client = new GuzzleHttpClient();
$response = $client->request('POST', $url, [
'form_params' => [
'username' => 'test',
'password' => 'test',
]
]);

return $this->render('esterne/post.html.twig', array(
'response'=>$response,
));
}


this is the twig file post.html.twig



{{response}}


the result is this:



{"status":"200","data":{"is_auth":true,"userToken":"194b873c004716acb3e0a5fba09fe405"}}


but if I put in html:



return $this->render('esterne/post.html.twig', array(
'response'=>$response->getBody(),
));


it results in error 500 internal server error




[2018-11-14 09:56:35] request.CRITICAL: Uncaught PHP Exception Twig_Error_Runtime: "An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class GuzzleHttpPsr7Response could not be converted to string")." at /app/app/Resources/views/esterne/post.html.twig line 1 {"exception":"[object] (Twig_Error_Runtime(code: 0): An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class GuzzleHttpPsr7Response could not be converted to string"). at /app/app/Resources/views/esterne/post.html.twig:1, ErrorException(code: 0): Catchable Fatal Error: Object of class GuzzleHttpPsr7Response could not be converted to string at /app/var/cache/prod/twig/47/478ca9f9b0a5c69caa7b0fed874bf831466230764635f396f057dc2c33868549.php:23)"}




SOLUTION



use file



{{ response|json_encode()|raw }}


in twig and



return $this->render('esterne/post.html.twig', array(
'response'=>json_decode($response->getBody()->getContents(), FALSE),
));









share|improve this question

























  • Convert your response to json response .

    – Rohan Kumar Shrestha
    Nov 14 '18 at 10:01











  • like this ?? Creating a JSON Response¶ symfony.com/doc/current/components/http_foundation.html

    – cris
    Nov 14 '18 at 10:34


















0















I want to make a post call with rest api using guzzlehttp in symfony ...
I wrote this code but the response



/**
* @Route("/post/")
*/
public function postAction()
{
$client = new GuzzleHttpClient();
$response = $client->request('POST', $url, [
'form_params' => [
'username' => 'test',
'password' => 'test',
]
]);

return $this->render('esterne/post.html.twig', array(
'response'=>$response,
));
}


this is the twig file post.html.twig



{{response}}


the result is this:



{"status":"200","data":{"is_auth":true,"userToken":"194b873c004716acb3e0a5fba09fe405"}}


but if I put in html:



return $this->render('esterne/post.html.twig', array(
'response'=>$response->getBody(),
));


it results in error 500 internal server error




[2018-11-14 09:56:35] request.CRITICAL: Uncaught PHP Exception Twig_Error_Runtime: "An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class GuzzleHttpPsr7Response could not be converted to string")." at /app/app/Resources/views/esterne/post.html.twig line 1 {"exception":"[object] (Twig_Error_Runtime(code: 0): An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class GuzzleHttpPsr7Response could not be converted to string"). at /app/app/Resources/views/esterne/post.html.twig:1, ErrorException(code: 0): Catchable Fatal Error: Object of class GuzzleHttpPsr7Response could not be converted to string at /app/var/cache/prod/twig/47/478ca9f9b0a5c69caa7b0fed874bf831466230764635f396f057dc2c33868549.php:23)"}




SOLUTION



use file



{{ response|json_encode()|raw }}


in twig and



return $this->render('esterne/post.html.twig', array(
'response'=>json_decode($response->getBody()->getContents(), FALSE),
));









share|improve this question

























  • Convert your response to json response .

    – Rohan Kumar Shrestha
    Nov 14 '18 at 10:01











  • like this ?? Creating a JSON Response¶ symfony.com/doc/current/components/http_foundation.html

    – cris
    Nov 14 '18 at 10:34
















0












0








0








I want to make a post call with rest api using guzzlehttp in symfony ...
I wrote this code but the response



/**
* @Route("/post/")
*/
public function postAction()
{
$client = new GuzzleHttpClient();
$response = $client->request('POST', $url, [
'form_params' => [
'username' => 'test',
'password' => 'test',
]
]);

return $this->render('esterne/post.html.twig', array(
'response'=>$response,
));
}


this is the twig file post.html.twig



{{response}}


the result is this:



{"status":"200","data":{"is_auth":true,"userToken":"194b873c004716acb3e0a5fba09fe405"}}


but if I put in html:



return $this->render('esterne/post.html.twig', array(
'response'=>$response->getBody(),
));


it results in error 500 internal server error




[2018-11-14 09:56:35] request.CRITICAL: Uncaught PHP Exception Twig_Error_Runtime: "An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class GuzzleHttpPsr7Response could not be converted to string")." at /app/app/Resources/views/esterne/post.html.twig line 1 {"exception":"[object] (Twig_Error_Runtime(code: 0): An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class GuzzleHttpPsr7Response could not be converted to string"). at /app/app/Resources/views/esterne/post.html.twig:1, ErrorException(code: 0): Catchable Fatal Error: Object of class GuzzleHttpPsr7Response could not be converted to string at /app/var/cache/prod/twig/47/478ca9f9b0a5c69caa7b0fed874bf831466230764635f396f057dc2c33868549.php:23)"}




SOLUTION



use file



{{ response|json_encode()|raw }}


in twig and



return $this->render('esterne/post.html.twig', array(
'response'=>json_decode($response->getBody()->getContents(), FALSE),
));









share|improve this question
















I want to make a post call with rest api using guzzlehttp in symfony ...
I wrote this code but the response



/**
* @Route("/post/")
*/
public function postAction()
{
$client = new GuzzleHttpClient();
$response = $client->request('POST', $url, [
'form_params' => [
'username' => 'test',
'password' => 'test',
]
]);

return $this->render('esterne/post.html.twig', array(
'response'=>$response,
));
}


this is the twig file post.html.twig



{{response}}


the result is this:



{"status":"200","data":{"is_auth":true,"userToken":"194b873c004716acb3e0a5fba09fe405"}}


but if I put in html:



return $this->render('esterne/post.html.twig', array(
'response'=>$response->getBody(),
));


it results in error 500 internal server error




[2018-11-14 09:56:35] request.CRITICAL: Uncaught PHP Exception Twig_Error_Runtime: "An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class GuzzleHttpPsr7Response could not be converted to string")." at /app/app/Resources/views/esterne/post.html.twig line 1 {"exception":"[object] (Twig_Error_Runtime(code: 0): An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class GuzzleHttpPsr7Response could not be converted to string"). at /app/app/Resources/views/esterne/post.html.twig:1, ErrorException(code: 0): Catchable Fatal Error: Object of class GuzzleHttpPsr7Response could not be converted to string at /app/var/cache/prod/twig/47/478ca9f9b0a5c69caa7b0fed874bf831466230764635f396f057dc2c33868549.php:23)"}




SOLUTION



use file



{{ response|json_encode()|raw }}


in twig and



return $this->render('esterne/post.html.twig', array(
'response'=>json_decode($response->getBody()->getContents(), FALSE),
));






symfony guzzle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 12:51







cris

















asked Nov 14 '18 at 9:58









criscris

14




14













  • Convert your response to json response .

    – Rohan Kumar Shrestha
    Nov 14 '18 at 10:01











  • like this ?? Creating a JSON Response¶ symfony.com/doc/current/components/http_foundation.html

    – cris
    Nov 14 '18 at 10:34





















  • Convert your response to json response .

    – Rohan Kumar Shrestha
    Nov 14 '18 at 10:01











  • like this ?? Creating a JSON Response¶ symfony.com/doc/current/components/http_foundation.html

    – cris
    Nov 14 '18 at 10:34



















Convert your response to json response .

– Rohan Kumar Shrestha
Nov 14 '18 at 10:01





Convert your response to json response .

– Rohan Kumar Shrestha
Nov 14 '18 at 10:01













like this ?? Creating a JSON Response¶ symfony.com/doc/current/components/http_foundation.html

– cris
Nov 14 '18 at 10:34







like this ?? Creating a JSON Response¶ symfony.com/doc/current/components/http_foundation.html

– cris
Nov 14 '18 at 10:34














1 Answer
1






active

oldest

votes


















0














You could try following response.



return $this->render('esterne/post.html.twig', array(
'response'=>$response->getBody()->getContent(),
));





share|improve this answer
























  • error... not work 2018-11-14T10:20:40.804640+00:00 app[web.1]: [2018-11-14 10:20:40] php.CRITICAL: Call to undefined method GuzzleHttpPsr7Stream::getContent() {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Call to undefined method GuzzleHttp\Psr7\Stream::getContent() at /app/src/AppBundle/Controller/EsterneController.php:109)"}

    – cris
    Nov 14 '18 at 10:22













  • ->getContents() should be used instead of ->getContent()

    – sklwebdev
    Nov 14 '18 at 10:29













  • now no error.... but the result is not in json ... view source of the page is html {"status":"200","data":{"is_auth":true,"userToken":"9a1d168594a9755d849abe283b3505ea"}}

    – cris
    Nov 14 '18 at 10:36













  • you should be used instead of above. json_decode($response->getBody(), true); or json_decode($response->getBody()->getContents(), true)

    – ercvs
    Nov 14 '18 at 11:24











  • ok ... in both case the result is json but in the response appear Array ... why?

    – cris
    Nov 14 '18 at 11:48













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297432%2fguzzlehttp-and-symfony-with-rest-api-problem%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You could try following response.



return $this->render('esterne/post.html.twig', array(
'response'=>$response->getBody()->getContent(),
));





share|improve this answer
























  • error... not work 2018-11-14T10:20:40.804640+00:00 app[web.1]: [2018-11-14 10:20:40] php.CRITICAL: Call to undefined method GuzzleHttpPsr7Stream::getContent() {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Call to undefined method GuzzleHttp\Psr7\Stream::getContent() at /app/src/AppBundle/Controller/EsterneController.php:109)"}

    – cris
    Nov 14 '18 at 10:22













  • ->getContents() should be used instead of ->getContent()

    – sklwebdev
    Nov 14 '18 at 10:29













  • now no error.... but the result is not in json ... view source of the page is html {"status":"200","data":{"is_auth":true,"userToken":"9a1d168594a9755d849abe283b3505ea"}}

    – cris
    Nov 14 '18 at 10:36













  • you should be used instead of above. json_decode($response->getBody(), true); or json_decode($response->getBody()->getContents(), true)

    – ercvs
    Nov 14 '18 at 11:24











  • ok ... in both case the result is json but in the response appear Array ... why?

    – cris
    Nov 14 '18 at 11:48


















0














You could try following response.



return $this->render('esterne/post.html.twig', array(
'response'=>$response->getBody()->getContent(),
));





share|improve this answer
























  • error... not work 2018-11-14T10:20:40.804640+00:00 app[web.1]: [2018-11-14 10:20:40] php.CRITICAL: Call to undefined method GuzzleHttpPsr7Stream::getContent() {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Call to undefined method GuzzleHttp\Psr7\Stream::getContent() at /app/src/AppBundle/Controller/EsterneController.php:109)"}

    – cris
    Nov 14 '18 at 10:22













  • ->getContents() should be used instead of ->getContent()

    – sklwebdev
    Nov 14 '18 at 10:29













  • now no error.... but the result is not in json ... view source of the page is html {"status":"200","data":{"is_auth":true,"userToken":"9a1d168594a9755d849abe283b3505ea"}}

    – cris
    Nov 14 '18 at 10:36













  • you should be used instead of above. json_decode($response->getBody(), true); or json_decode($response->getBody()->getContents(), true)

    – ercvs
    Nov 14 '18 at 11:24











  • ok ... in both case the result is json but in the response appear Array ... why?

    – cris
    Nov 14 '18 at 11:48
















0












0








0







You could try following response.



return $this->render('esterne/post.html.twig', array(
'response'=>$response->getBody()->getContent(),
));





share|improve this answer













You could try following response.



return $this->render('esterne/post.html.twig', array(
'response'=>$response->getBody()->getContent(),
));






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 10:11









ercvsercvs

5810




5810













  • error... not work 2018-11-14T10:20:40.804640+00:00 app[web.1]: [2018-11-14 10:20:40] php.CRITICAL: Call to undefined method GuzzleHttpPsr7Stream::getContent() {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Call to undefined method GuzzleHttp\Psr7\Stream::getContent() at /app/src/AppBundle/Controller/EsterneController.php:109)"}

    – cris
    Nov 14 '18 at 10:22













  • ->getContents() should be used instead of ->getContent()

    – sklwebdev
    Nov 14 '18 at 10:29













  • now no error.... but the result is not in json ... view source of the page is html {"status":"200","data":{"is_auth":true,"userToken":"9a1d168594a9755d849abe283b3505ea"}}

    – cris
    Nov 14 '18 at 10:36













  • you should be used instead of above. json_decode($response->getBody(), true); or json_decode($response->getBody()->getContents(), true)

    – ercvs
    Nov 14 '18 at 11:24











  • ok ... in both case the result is json but in the response appear Array ... why?

    – cris
    Nov 14 '18 at 11:48





















  • error... not work 2018-11-14T10:20:40.804640+00:00 app[web.1]: [2018-11-14 10:20:40] php.CRITICAL: Call to undefined method GuzzleHttpPsr7Stream::getContent() {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Call to undefined method GuzzleHttp\Psr7\Stream::getContent() at /app/src/AppBundle/Controller/EsterneController.php:109)"}

    – cris
    Nov 14 '18 at 10:22













  • ->getContents() should be used instead of ->getContent()

    – sklwebdev
    Nov 14 '18 at 10:29













  • now no error.... but the result is not in json ... view source of the page is html {"status":"200","data":{"is_auth":true,"userToken":"9a1d168594a9755d849abe283b3505ea"}}

    – cris
    Nov 14 '18 at 10:36













  • you should be used instead of above. json_decode($response->getBody(), true); or json_decode($response->getBody()->getContents(), true)

    – ercvs
    Nov 14 '18 at 11:24











  • ok ... in both case the result is json but in the response appear Array ... why?

    – cris
    Nov 14 '18 at 11:48



















error... not work 2018-11-14T10:20:40.804640+00:00 app[web.1]: [2018-11-14 10:20:40] php.CRITICAL: Call to undefined method GuzzleHttpPsr7Stream::getContent() {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Call to undefined method GuzzleHttp\Psr7\Stream::getContent() at /app/src/AppBundle/Controller/EsterneController.php:109)"}

– cris
Nov 14 '18 at 10:22







error... not work 2018-11-14T10:20:40.804640+00:00 app[web.1]: [2018-11-14 10:20:40] php.CRITICAL: Call to undefined method GuzzleHttpPsr7Stream::getContent() {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Call to undefined method GuzzleHttp\Psr7\Stream::getContent() at /app/src/AppBundle/Controller/EsterneController.php:109)"}

– cris
Nov 14 '18 at 10:22















->getContents() should be used instead of ->getContent()

– sklwebdev
Nov 14 '18 at 10:29







->getContents() should be used instead of ->getContent()

– sklwebdev
Nov 14 '18 at 10:29















now no error.... but the result is not in json ... view source of the page is html {"status":"200","data":{"is_auth":true,"userToken":"9a1d168594a9755d849abe283b3505ea"}}

– cris
Nov 14 '18 at 10:36







now no error.... but the result is not in json ... view source of the page is html {"status":"200","data":{"is_auth":true,"userToken":"9a1d168594a9755d849abe283b3505ea"}}

– cris
Nov 14 '18 at 10:36















you should be used instead of above. json_decode($response->getBody(), true); or json_decode($response->getBody()->getContents(), true)

– ercvs
Nov 14 '18 at 11:24





you should be used instead of above. json_decode($response->getBody(), true); or json_decode($response->getBody()->getContents(), true)

– ercvs
Nov 14 '18 at 11:24













ok ... in both case the result is json but in the response appear Array ... why?

– cris
Nov 14 '18 at 11:48







ok ... in both case the result is json but in the response appear Array ... why?

– cris
Nov 14 '18 at 11:48




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297432%2fguzzlehttp-and-symfony-with-rest-api-problem%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly