Skip to main content
Not every request needs to trigger an Inertia page visit. For calls to an external API or fetching data from a non-Inertia endpoint, the useHttp hook provides the same developer experience as useForm, but for standalone HTTP requests.

Basic Usage

The useHttp hook accepts initial data and returns reactive state along with methods for making HTTP requests.
Unlike router visits, useHttp requests do not trigger page navigation or interact with Inertia’s page lifecycle. They are plain HTTP requests that return JSON responses.

Submitting Data

The hook provides get, post, put, patch, and delete convenience methods. A generic submit method is also available for dynamic HTTP methods.
Each method returns a Promise that resolves with the parsed JSON response data. TypeScript users may type the request data and response.

Pre-binding the Endpoint

You may pass the HTTP method and URL as the first two arguments to useHttp(), then call submit() without arguments to dispatch the request. This also unlocks real-time validation. See Precognition for details.

Multiple Requests

Each useHttp instance tracks its own processing, errors, and other reactive state. When making independent requests, you may create a separate instance for each one so their states don’t collide.

Reactive State

The useHttp hook exposes the same reactive properties as useForm:

Validation Errors

When a request returns a 422 status code, the hook automatically parses validation errors and makes them available through the errors property.

Displaying All Errors

By default, validation errors are simplified to the first error message for each field. You may chain withAllErrors() to receive all error messages as arrays, which is useful for fields with multiple validation rules.
The same method is available on the useForm helper and the <Form> component.

File Uploads

When the data includes files, the hook automatically sends the request as multipart/form-data. Upload progress is available through the progress property.

Cancelling Requests

You may cancel an in-progress request using the cancel() method.

Optimistic Updates

The useHttp hook supports optimistic updates via the optimistic() method. The callback receives the current data and should return a partial update to apply immediately.
The update is applied synchronously. If the request fails, the data is rolled back to its previous state.

Event Callbacks

Each submit method accepts an options object with lifecycle callbacks:
You may return false from onBefore to cancel the request.

Accessing the HTTP response

The onSuccess callback receives the parsed response data as its first argument and the HTTP response object as its second argument. The response object contains status, data, and headers properties, which is useful when you need to inspect the status code or response headers.

HTTP exceptions

The onHttpException callback fires when the server returns a non-422 HTTP error, such as a 500 or 403. The callback receives the HTTP response object, giving you access to the status code, response body, and headers.

Network errors

The onNetworkError callback fires when the request fails due to a network issue, such as when the user loses their internet connection. The callback receives the Error object.

Precognition

The useHttp hook supports Laravel Precognition for real-time validation. Enable it by chaining withPrecognition() with the HTTP method and validation endpoint.
Once enabled, the validate(), touch(), touched(), valid(), and invalid() methods become available, working identically to form precognition.

History State

You may persist data and errors in browser history state by providing a remember key as the first argument.
You may exclude sensitive fields from being stored in history state using the dontRemember() method.