Skip to content
Advertisement

Guzzle query string prameters

I’m having problems writing my request. I need to provide query string parameters to GET method using request(). Since I’m writing tests I can’t edit the method.

Right now I have it written like this:

$result = $client->request(
                'GET',
                'cart/current/get-items',
                [
                    'headers' => [
                        'X-AUTH-API-TOKEN' => 'Bearer ' . $tokens->token
                    ],
                    'query'=>[
                        'paginator_data'=>[
                            'page'=>1,
                            'page_size' => 10
                        ]
                    ]
                ]

Method that gets paginator_data:

 $paginatorData = json_decode($request->query->get('paginator_data'), true);

And thats how query looks if I check the request testing it from the front-end:

Source:

paginator_data=%7B%22page%22:1,%22page_size%22:10%7D

Parsed:

paginator_data: {"page":1,"page_size":10}

how do I format the query to make it look like the Source one? Because if I copy paste it inside my query it works.

Advertisement

Answer

I think you maybe need to json_encode your data:

$result = $client->request(
    'GET',
    'cart/current/get-items',
    [
        'headers' => [
            'X-AUTH-API-TOKEN' => 'Bearer ' . $tokens->token
        ],
        'query'=>[
            'paginator_data'=> json_encode([
                'page'=>1,
                'page_size' => 10
            ])
        ]
    ]
);

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement