Guzzle form_params not accepting array

Petyo Tsonev picture Petyo Tsonev · Sep 1, 2015 · Viewed 7.9k times · Source

I am using Guzzle 6 and I can't pass array with form_params in the body of the client

$postFields = [
    form_params => [
        'data[test]' => "TEST",
        'data[whatever]' => "Whatever..."
    ]
];

$client = new GuzzleClient([
        'cookies' => $jar, // The cookie
        'allow_redirects' => true, // Max 5 Redirects
        'base_uri' => $this->navigateUrl, // Base Uri
        'headers' => $this->headers
]);
$response = $client->post('api',[$postFields]);

Finally, when i send the request my data is gone... But if I manually add the the data in the response it's working fine.

$response = $client->post(
    'api',
    [form_params => [
        'data[test]'=>"TEST",
        'data[wht]' => 'Whatever'
    ],
]
// It's working this way...

I hope I am clear enough if u need more info feel free to ask. Thanks in advance.

Answer

Shaun Bramley picture Shaun Bramley · Sep 30, 2015

I see a couple of issues. First is the fact that your $postFields array does not appear to be properly formatted, and second you wrap your $postFields array inside another array.

$options = [
    'debug' => true,
    'form_params' => [
        'test' => 15,
        'id' => 43252435243654352,
        'name' => 'this is a random name',
    ],
    'on_stats' => $someCallableItem,
];
$response = $client->post('api', $options);