HTTP Response

HTMX allows your response to actually change the behavior of the initial request. For example, your server-side logic can perform an action like:

  • Redirect the request to a new page

  • Retarget the area of the page to place the new content with

  • Trigger another event on the page

  • Tell the page to reload

  • Push a new URL to the history stack

To enable all of this logic we created a special Lava command to interact with the response in a robust way.

Basic Example

The example below would tell the page to reload itself.

{% httpresponse %}
  [[ header key:'HX-Refresh' ]]true[[ endheader ]]
{% endhttpresponse %}

Note that multiple headers can be provided at once.

Changing the Response Status

By default the HTTP response status will be 200 when your call is matched and run successfully. You might want to change that based on the logic of your endpoint. For instance, you may want to return a 404 status if the group you are updating was not found. Below is an example of updating the return status.

{% httpresponse status:'404' %}
{% endhttpresponse %}

Last updated