kitetail / zttp

A developer-experience focused HTTP client, optimized for most common use cases.
MIT License
1.68k stars 120 forks source link

Sending [] in URL Query Params get encoded #88

Open ajtrichards opened 4 years ago

ajtrichards commented 4 years ago

I'm trying to use this library to interact with the Zendesk API. Using the following code, I'm requesting the Users endpoint with some query params in the URL ?role[]=agent&role[]=admin

However, when I have this code I get an error response back from Zendesk saying the params aren't correct.

Request to API

$req = Zttp::withHeaders([
    'Authorization' => 'Bearer MYTOKEN',
])->get('https://MYSUBDOMAIN.zendesk.com/api/v2/users.json?role[]=agent&role[]=admin');

dd($req->json());

Error from API

^ array:1 [▼
  "error" => array:2 [▼
    "title" => "Invalid attribute"
    "message" => "You passed an invalid value for the role attribute. Invalid parameter: role must be a string from api/v2/users/index"
  ]
]

If I look at the URL which Zttp actually sends it appears as: https://MYSUBDOMAIN.zendesk.com/api/v2/users.json?role%5B0%5D=admin&role%5B1%5D=agent

I have also tried using:

    $req = Zttp::withHeaders([
        'Authorization' => 'Bearer MYTOKEN',
    ])->get('https://MYSUBDOMAIN.zendesk.com/api/v2/users.json', [
        'role' => 'admin',
        'role' => 'agent',
    ]);

and

    $req = Zttp::withHeaders([
        'Authorization' => 'Bearer MYTOKEN',
    ])->get('https://MYSUBDOMAIN.zendesk.com/api/v2/users.json', [
        'role[]' => 'admin',
        'role[]' => 'agent',
    ]);

Using this approach, Zendesk get's only one of the parameters. Does anyone have any suggestions how I can get this working? Via the query params is the only way Zendesk will accept.

jonbaldie commented 3 years ago

Have you tried sending the roles as an array instead?

$req = Zttp::withHeaders([
    'Authorization' => 'Bearer MYTOKEN',
])->get('https://MYSUBDOMAIN.zendesk.com/api/v2/users.json', [
    'role' => ['admin',  'agent'],
]);

Those last two examples won't work because they're defining duplicate keys on a PHP array.