Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guzzle Post Null/Empty Values in Laravel

I've been trying to work with Guzzle and learn my way around it, but I'm a bit confused about using a request in conjunction with empty or null values.

For example:

$response = $client->request('POST', 
    'https://www.testsite.com/coep/public/api/donations', [
    'form_params' => [
        'client' => [
            'web_id' => NULL,
            'name' => 'Test Name',
            'address1' => '123 E 45th Avenue',
            'address2' => 'Ste. 1',
            'city' => 'Nowhere',
            'state' => 'CO',
            'zip' => '80002'
        ],
        'contact' => [],
        'donation' => [
            'status_id' => 1,
            'amount' => $donation->amount,
            'balance' => $donation->balance,
            'date' => $donation->date,
            'group_id' => $group->id,
        ],
    ]
]);

After running a test, I found out that 'web_id' completely disappears from my request if set to NULL. My question is how do I ensure that it is kept around on the request to work with my conditionals?

At this point, if I dd the $request->client, all I get back is everything but the web_id. Thanks!

like image 463
inuShiva Avatar asked Jan 23 '19 18:01

inuShiva


1 Answers

I ran into this issue yesterday and your question is very well ranked on Google. Shame that it has no answer.

The problem here is that form_params uses http_build_query() under the hood and as stated in this user contributed note, null params are not present in the function's output.

I suggest that you pass this information via a JSON body (by using json as key instead of form_params) or via multipart (by using multipart instead of form_params).

Note: those 2 keys are available as constants, respectively GuzzleHttp\RequestOptions::JSON and GuzzleHttp\RequestOptions::MULTIPART

like image 115
Damien Avatar answered Nov 12 '22 10:11

Damien