Building a Laravel Package Upgrade Checklist
When you maintain a Laravel package, an upgrade checklist helps you verify that a new release supports the Laravel and PHP versions…
When Laravel code needs data from another web service, the HTTP client sends an outgoing request and gives your code a response object to inspect. A GET request typically asks for data, while a POST request sends data to the service. Laravel’s Http facade provides both methods through a compact interface built around the Guzzle HTTP client.
The client sends a GET request with values in the URL, called query parameters, or a POST request with a data array. Laravel sends POST data as JSON by default, and its response object provides methods for reading the body, checking the status code, and deciding how to handle errors.
Http::get() sends an HTTP GET request to the URL you provide. A GET request can include query parameters to filter or paginate results. Pass those values as the second argument, or include them in the URL yourself. Laravel documents both forms in its HTTP client reference.
use Illuminate\Support\Facades\Http;
$response = Http::get('https://api.example.com/products', [
'category' => 'books',
'page' => 2,
]);
Laravel adds the supplied values to the request URL as a query string. For reusable request setup, call withQueryParameters() before sending the request:
$response = Http::withQueryParameters([
'category' => 'books',
'page' => 2,
])->get('https://api.example.com/products');
The facade starts a request and returns an Illuminate\Http\Client\Response object. That object represents the remote service’s response; it is separate from Laravel’s incoming-request class, which handles requests sent to your own application.
Http::post() sends a request with the data array you pass as its second argument. By default, Laravel sets the content type to application/json, a label that tells the receiving service to read the request body as JSON.
$response = Http::post('https://api.example.com/products', [
'name' => 'Notebook',
'price' => 12.50,
]);
The array describes the data Laravel sends. The remote service must accept JSON and expect fields with matching names. To ask for a JSON response, set the Accept header with acceptJson():
$response = Http::acceptJson()
->post('https://api.example.com/products', [
'name' => 'Notebook',
'price' => 12.50,
]);
The Accept header tells the service what response format the client prefers. It does not determine the format of the outgoing request body. If the service expects form-encoded data instead of JSON, call asForm() before sending the request. Laravel also supports custom headers through withHeaders() and raw request bodies through withBody(), as described in the Laravel HTTP client documentation.
Laravel returns a response object after the remote service replies. Use status() to read the HTTP status code, the number that reports how the service handled the request. You can also use helpers such as successful(), clientError(), and serverError() to check the result:
$response = Http::get('https://api.example.com/products');
if ($response->successful()) {
$products = $response->json();
} elseif ($response->clientError()) {
// The service returned a 4xx status.
} elseif ($response->serverError()) {
// The service returned a 5xx status.
}
successful() checks for a successful status, while clientError() and serverError() identify 4xx and 5xx responses. The client does not throw an exception just because the service returns one of those error statuses. Check the response or call throw() when your code should handle those statuses as exceptions.
For a JSON response, json() converts the response data into a PHP array by default. It also accepts a key and a default value, which lets you read a field without first storing the entire response:
$name = $response->json('name', 'Unknown');
Use body() to read the raw response body as a string. Use object() when you want decoded JSON as an object, or collect() when you want an Illuminate\Support\Collection. The response also supports array access for JSON data, as shown in Laravel’s response inspection documentation. If the response body is not valid JSON, json() can return null; check the response format before relying on a field.
A 4xx or 5xx status produces a response object that your code can inspect. A timeout or other connection failure throws a ConnectionException. Laravel’s default timeout is 30 seconds; set a limit explicitly when your request should stop waiting sooner:
$response = Http::timeout(5)
->get('https://api.example.com/products');
The PendingRequest API documents request configuration such as timeouts. Choose a timeout that fits the request’s role, and catch ConnectionException if your code needs to handle network failures separately from HTTP error responses.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com