Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rohansinghrawat's avatar

How to determine whether to use query parameters or body in GET requests when integrating with different APIs?

I am working with two different APIs, both using GET requests but handling request data differently:

  1. API 1 requires the data to be sent in the request body, even though the HTTP method is GET.
    Example:

    $config = [
        'method' => 'GET',
        'url' => 'http://example.com/api/endpoint',
        'headers' => [
            'Content-Type' => 'application/json',
        ],
        'body' => [
            'key1' => 'value1',
            'key2' => 'value2',
        ],
    ];
    
  2. API 2 requires the data to be sent as query parameters in the URL.
    Example:

    $config = [
        'method' => 'GET',
        'url' => 'http://example.com/api/endpoint',
        'headers' => [
            'Content-Type' => 'application/json',
        ],
        'query' => [
            'key1' => 'value1',
            'key2' => 'value2',
        ],
    ];
    

Both APIs work with the GET method, but their requirements for passing data are different. This makes it challenging to decide when to use query parameters and when to use the request body for a GET request.

Questions:

  1. How can I determine whether to use query parameters (query) or the request body (body) for a GET request?
  2. Is it common or correct for a GET request to accept data in the body, even though the HTTP specification discourages it?
  3. How can I design a generic HTTP client (e.g., using Guzzle) to handle these differences dynamically based on the API's requirements?

Any guidance, best practices, or examples would be appreciated!

0 likes
5 replies
jlrdw's avatar

Not sure about guzzle, but in axios js you can have a custom function and return whether body or query.

Or just use body for both if possible.

Tray2's avatar

For me it depends on the amount of parameters, if there is like one then I just put it on the url

https://somesite.somedomain/api/someendpoint/myvalue

It there are more than that I usually just

let parameters = {
	param1: 'somevalue',
	param2: 'someothervalue'
};

//more code here

let responsePromise = await fetch(url, { method: 'GET', headers: parameters});
rohansinghrawat's avatar

@Tray2 the problem here is API 1 is working only with "body" options and API2 is working with "query" option also 1st is accepting json another one is accepting array so problm here is I am not able to determine when to use which as I have created one baseHttpClient to processs all kinds of request but this scenario fails my GET request handling

rohansinghrawat's avatar

@Tray2 the first one gives error when sending query and second one gives error if sending body

Please or to participate in this conversation.