Passing object to body

65 views
Skip to first unread message

Random Guy

unread,
Apr 24, 2014, 7:19:20 AM4/24/14
to guz...@googlegroups.com
Hey all,

I am calling a REST API using Guzzle, but I am struggling a little bit with passing a JSON object.



                $request = $client->post('rest/user/current/resources/test', [
                                         'auth' => [self::USERNAME, self::PASSWORD],
                                         'body' => json_encode(['skeletonTest' => $skeletonTest])
                                        ]);

Providing the above piece of code, I am getting an internal Exception which suggests that the passed Test object ($skeletonTest in this scenario)  is null.
Given that $skeletonTest is not null, because I am populating it on the previous lines, I am assuming that I am not making correct use of the "body".

Any help would be highly appreciated.


Thanks

Michael Dowling

unread,
Apr 24, 2014, 7:30:43 AM4/24/14
to guz...@googlegroups.com
Perhaps json_encode is failing to encode the value?
--
You received this message because you are subscribed to the Google Groups "Guzzle - PHP HTTP client and REST client framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guzzle+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Random Guy

unread,
Apr 24, 2014, 7:38:36 AM4/24/14
to guz...@googlegroups.com
Hey Michael,

thanks for getting back to me.

May this be also a possibility? How could I double-check that ?


Also, the docs suggest that :

-d 'skeletonTest={skeletonTest}'

Correct me if I am wrong, but presumably that means that the line:


'body' => json_encode(['skeletonTest' => $skeletonTest])

is fine (besides the json_encode() failure possibility).



Thanks again.

Michael Dowling

unread,
Apr 24, 2014, 8:20:18 AM4/24/14
to guz...@googlegroups.com
Can you provide the stack trace?

Random Guy

unread,
Apr 24, 2014, 8:45:18 AM4/24/14
to guz...@googlegroups.com

Sure:

HTTP/1.1 400 Bad Request
Date: Thu, 24 Apr 2014 12:43:43 GMT
Access-control-allow-credentials: true
Content-type: application/json; charset=UTF-8
Access-control-allow-origin: *
Access-control-allow-methods: GET, POST, PUT, DELETE, OPTIONS, HEAD
Connection: close
Transfer-Encoding: chunked

{"message":"VDC cannot be null","faultInfo":{"message":"Test cannot be null","errorType":"ErrorInvalidInput","errorCode":"TEST_CANNOT_BE_NULL"},"errorType":"ErrorInvalidInput","errorCode":"TEST_CANNOT_BE_NULL","httpErrorCode":400}End of createTest
PHP Notice:  Undefined variable: response in /Applications/MAMP/htdocs/index.php on line 188
PHP Stack trace:
PHP   1. {main}() /Applications/MAMP/htdocs/index.php:0
PHP   2. RESTclient->__construct() /Applications/MAMP/htdocs/index.php:194
PHP   3. RESTclient->execute() /Applications/MAMP/htdocs/index.php:26
PHP   4. RESTclient->createVDC() /Applications/MAMP/htdocs/index.php:38

Notice: Undefined variable: response in /Applications/MAMP/htdocs/index.php on line 188

Call Stack:
    0.0004     254904   1. {main}() /Applications/MAMP/htdocs/index.php:0
    0.0136     403736   2. RESTclient->__construct() /Applications/MAMP/htdocs/index.php:194
    0.0227    1078208   3. RESTclient->execute() /Applications/MAMP/htdocs/index.php:26
    0.4774    1649736   4. RESTclient->createVDC() /Applications/MAMP/htdocs/index.php:38

PHP Fatal error:  Call to a member function getBody() on a non-object in /Applications/MAMP/htdocs/index.php on line 188
PHP Stack trace:
PHP   1. {main}() /Applications/MAMP/htdocs/index.php:0
PHP   2. RESTclient->__construct() /Applications/MAMP/htdocs/index.php:194
PHP   3. RESTclient->execute() /Applications/MAMP/htdocs/index.php:26
PHP   4. RESTclient->createVDC() /Applications/MAMP/htdocs/index.php:38

Fatal error: Call to a member function getBody() on a non-object in /Applications/MAMP/htdocs/index.php on line 188

Call Stack:
    0.0004     254904   1. {main}() /Applications/MAMP/htdocs/index.php:0
    0.0136     403736   2. RESTclient->__construct() /Applications/MAMP/htdocs/index.php:194
    0.0227    1078208   3. RESTclient->execute() /Applications/MAMP/htdocs/index.php:26
    0.4774    1649736   4. RESTclient->createVDC() /Applications/MAMP/htdocs/index.php:38
Reply all
Reply to author
Forward
0 new messages