Question

How do i get post data through the guzzle service clients getCommand function?

my json looks like the following:

    "createMessage": {
        "httpMethod": "POST",
        "uri": "conversations/{conversation_id}/message",
        "summary": "conversations by user",
        "responseType": "class",
        "responseClass": "\\Conversations\\Message",
        "parameters": {
            "conversation_id": {
                "location": "uri",
                "description": "conversation id",
                "type": "integer"
             },
             "message": {
                 "location": "postField",
                 "sentAs": "message",
                 "type": "string"
             }
         }
     }

then i currently put my post data as part of the array passed through the getCommand:

$client = new \Guzzle\Service\Client();
$client->setDescription(\Guzzle\Service\Description\ServiceDescription::factory(__DIR__ . '/client.json'));
$command = $client->getCommand('createMessage', array('conversation_id' => 6, 'message' => 'test post message'));

it creates the new record in the database but the post data is empty so the 'message' field is left empty.

i have tried setting $client->setPostField('message', 'test post message'); but doesn't appear to work.

Was it helpful?

Solution

Setting the content type to application/x-www-form-urlencoded appears to have done the trick, originally i had:

$command->set('command.headers', array('content-type' => 'application/json'));

However POST requests in Guzzle are sent with an application/x-www-form-urlencoded Content-Type

$command->set('command.headers', array('content-type' => 'application/x-www-form-urlencoded'));

alternatively you can also do this in the json schema, setting a parameter of content-type:

"content-type": {
    "location": "header",
    "default": "application/x-www-form-urlencoded"
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top