Sub topic 7
Request objects: params, headers, method and body
This chapter assumes you can write Angular components, read signals in templates, and know basic HTTP: methods, query parameters, headers and status codes. You should also have a working httpResource and provideHttpClient() in your app.
From a URL string to a request object
So far the request function returned a URL string. That is enough for a plain GET. When you need query parameters, headers or another method, return an object instead. This object is an HttpResourceRequest.
The fields you will use most are:
| Field | Purpose |
|---|---|
| `url` | The address to call. Required. |
| `method` | The HTTP method. Defaults to `GET`. |
| `params` | Query parameters, as an object. |
| `headers` | Request headers, as an object. |
| `body` | The request body, sent with methods such as `POST`. |
The request function is still reactive. Any signal you read inside it becomes a dependency. When one changes, the resource builds a new request object and sends it.
Query parameters
Put query parameters in params instead of building the query string by hand. Angular encodes the values for you.
import { Component, signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
interface Product {
id: number;
title: string;
}
@Component({
selector: 'app-products',
template: `
<button (click)="page.update((p) => p + 1)">Next page</button>
<ul>
@for (p of products.value(); track p.id) {
<li>{{ p.title }}</li>
}
</ul>
`,
})
export class ProductsComponent {
page = signal(1);
products = httpResource<Product[]>(
() => ({
url: '/api/products',
params: { page: this.page(), limit: 10 },
headers: { 'X-Client': 'tutorial-app' },
}),
{ defaultValue: [] },
);
}Clicking the button changes page. The resource then sends GET /api/products?page=2&limit=10 with the X-Client header. Parameter values can be strings, numbers or booleans. A value can also be an array, which sends the same key several times.
Method and body
Set method and body to send data with the request.
import { Component, signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
interface Report {
rows: number;
}
@Component({
selector: 'app-report',
template: `<p>Rows: {{ report.value()?.rows }}</p>`,
})
export class ReportComponent {
filters = signal({ region: 'eu', year: 2026 });
report = httpResource<Report>(() => ({
url: '/api/reports/search',
method: 'POST',
body: this.filters(),
}));
}This pattern fits a POST that only reads data, such as a search endpoint that takes a large filter object. Every time filters changes, the resource sends a new request.
Combining with undefined
The request object can be combined with the idle behavior from the previous chapter. Return undefined when the inputs are not ready, and return the object otherwise.
import { signal } from '@angular/core';
import { httpResource } from '@angular/common/http';
const token = signal<string | null>(null);
export const profile = httpResource<{ name: string }>(() => {
const t = token();
if (!t) {
return undefined;
}
return {
url: '/api/profile',
headers: { Authorization: `Bearer ${t}` },
};
});No request is sent until token holds a value. After that, each new token triggers a fresh request.
Common errors
- Reading a signal outside the request function. Only signals read inside the function are tracked. If you copy
this.page()into a plain variable earlier, changes will not trigger a request. - Building the query string by hand. Strings like `
/api/products?q=${q}skip encoding. A value containing&or#breaks the URL. Useparams`. - Using a write method for mutations. See the warning above.
- Forgetting that the object is rebuilt on every run. Create new values inside the function. That is fine, since the resource compares the resulting request, not your object's identity.
Exercise
You have a signal status = signal('open'). Write the request function for an httpResource that calls /api/tickets with the query parameter status set from the signal and the header Accept: application/json. What happens when you call status.set('closed')?
Show answer
The function is () => ({ url: '/api/tickets', params: { status: this.status() }, headers: { Accept: 'application/json' } }), using status() directly if it is not a class field. Because the signal is read inside the function, setting it to 'closed' reruns the function, and the resource cancels any in-flight request and sends GET /api/tickets?status=closed.