How to use curl GET instead of POST

How to use curl GET instead of POST

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to use curl GET instead of POST

When inspecting a remote service, you might encounter an API endpoint that expects data in the URL parameters rather than the request body. This is common with legacy systems or specific government services, such as the USDA eligibility checker referenced in recent queries. If your initial attempt using curl with the -X POST flag returns a 405 Method Not Allowed error or simply fails to trigger the response, the issue is almost certainly the HTTP method being used.

To resolve this, you need to switch from a POST request to a GET request. In PHP, using curl_setopt($ch, CURLOPT_POST, 1) tells the cURL library to send data in the body of the message. For a GET request, you must disable this option and instead pass the data as query string parameters appended to the URL.

Modifying the PHP cURL Configuration

In the provided example from the source material, the script constructs a large XML string stored in the $query variable. This is then passed to CURLOPT_POSTFIELDS. When switching to GET, you have two primary options depending on how strict the target server is regarding query string encoding.

Option 1: Pass Data via Query Parameters
If the API documentation explicitly states that it accepts parameters like street, city, and state in the URL, you should construct a query string. You can pass this to cURL using CURLOPT_HTTPGET (available in newer PHP versions) or by setting CURLOPT_POST to 0 and manually appending data to the URL via CURLOPT_URL.

$url = 'http://eligibility.cert.sc.egov.usda.gov/eligibility/eligibilityservice';
$queryString = "street={$street}&city={$city}&state={$state}&zip={$zip}";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '?' . $queryString);
curl_setopt($ch, CURLOPT_HTTPGET, true); // Explicitly sets method to GET
// Do NOT set CURLOPT_POST or CURLOPT_POSTFIELDS for a standard GET request
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); // Often required by remote servers

$return = curl_exec($ch);
curl_close($ch);

echo $return;

Option 2: Embed Data in the Request (if XML is required in URL)
The original code snippet suggests the server might expect the XML payload to be part of the request, but delivered via GET. While unusual for complex payloads like XML, some older CGI scripts treat $_GET as a flat map and reject anything resembling a body. If the server requires the XML structure to be sent but via GET, you must URL-encode the entire XML string and append it to the query string.

$xmlPayload = '<Eligibility><PropertyRequest StreetAddress1="'.$street.'" City="'.$city.'" ... />&lt;/PropertyRequest&gt;';
$url_encoded = rawurlencode($xmlPayload);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '?request=' . $url_encoded);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

Trade-offs and Security Considerations

Switching to a GET request changes how the data is handled by the underlying protocol. With POST, data resides in the request body, which has no theoretical size limit imposed by HTTP headers (though practical limits exist based on server configuration). With GET, data is part of the URL.

The primary limitation is character length. Most web browsers and servers impose a maximum URL length, typically around 2000 to 8000 characters depending on the browser and server configuration. If your eligibility check logic requires passing large blocks of text or complex XML structures via GET, you risk hitting this limit, causing the request to truncate or fail entirely.

Additionally, GET requests are cacheable by default. Browsers and intermediate proxies might store the result of a query based on its URL. In high-volume scenarios like batch eligibility checks, this can lead to stale data if the underlying service state changes dynamically. To prevent caching, always include a unique timestamp or random token in the query string parameters:

$url = $url . '?requestId=' . uniqid() . '&timestamp=' . time();

Finally, be mindful of URL encoding. Special characters within the XML payload (like <, >, &) must be encoded before being appended to the URL. Using rawurlencode() is safer than manual replacement to avoid breaking the query string syntax.

When to Use Which Approach

The decision to use GET over POST should never be arbitrary. It depends entirely on the server's expectation. If the server logs show a successful 200 OK when accessed via GET /endpoint?param=value, and returns a 405 error for POST, you must align your client code with the server's contract.

For modern RESTful APIs, POST is preferred for actions that modify state or handle large payloads. However, legacy systems often default to GET even for data retrieval involving parameters. Using curl in command-line terms confirms this distinction:

# Correct approach for a GET-only endpoint
curl -X GET "http://example.com/endpoint?street=123&city=NY"

If you are working within a Laravel application, the Http facade simplifies this logic without needing raw cURL configurations. You can dispatch a request with an explicit method:

$response = Http::get('http://eligibility.cert.sc.egov.usda.gov/eligibility/eligibilityservice', [
    'street' => $street,
    'city' => $city,
    'state' => $state,
    'zip' => $zip,
]);

echo $response->body();

Laravel handles the encoding and method switching internally, ensuring you adhere to the protocol requirements of the target service. Whether using raw cURL or the Laravel framework, always verify the Content-Type header and the HTTP method expected by the specific endpoint before executing the request.

Our network