Debian Codegraph @

www/src/content/reference/03-events/34-htmx-before-response.md

raw symbols SCIP
1 ---
2 title: "htmx:before:response"
3 description: "After a response is received but before the body is consumed"
4 ---
5
6 The `htmx:before:response` event fires after a fetch response arrives but before htmx reads the response body.
7
8 ## When It Fires
9
10 After the network response is received, before the body is consumed or any swap begins.
11
12 ## Event Detail
13
14 - `ctx.response.raw` - The raw Response object (body not yet consumed)
15 - `ctx.response.status` - The HTTP status code
16 - `ctx.response.headers` - The response headers
17
18 ## Example
19
20 ```javascript
21 htmx.on('htmx:before:response', (evt) => {
22 const status = evt.detail.ctx.response.status;
23 const headers = evt.detail.ctx.response.headers;
24
25 console.log('Response status:', status);
26 console.log('Content-Type:', headers.get('content-type'));
27
28 // Handle error status codes before body is consumed
29 if (status >= 400) {
30 console.log('Error response received');
31 }
32 });
33 ```
34
35 Cancel this event to skip normal response processing, including body consumption and the swap.