docs/docs/en/runjs/context/request.md
Initiate an authenticated HTTP request within RunJS. The request automatically carries the current application's baseURL, Token, locale, role, etc., and follows the application's request interception and error handling logic.
Applicable to any scenario in RunJS where a remote HTTP request needs to be initiated, such as JSBlock, JSField, JSItem, JSColumn, Workflow, Linkage, JSAction, etc.
request(options: RequestOptions): Promise<AxiosResponse<any>>;
RequestOptions extends Axios's AxiosRequestConfig:
type RequestOptions = AxiosRequestConfig & {
skipNotify?: boolean | ((error: any) => boolean); // Whether to skip global error prompts when the request fails
skipAuth?: boolean; // Whether to skip authentication redirection (e.g., do not redirect to login page on 401)
};
| Parameter | Type | Description |
|---|---|---|
url | string | Request URL. Supports resource style (e.g., users:list, posts:create), or a full URL |
method | 'get' | 'post' | 'put' | 'patch' | 'delete' | HTTP method, defaults to 'get' |
params | object | Query parameters, serialized into the URL |
data | any | Request body, used for post/put/patch |
headers | object | Custom request headers |
skipNotify | boolean | (error) => boolean | If true or the function returns true, global error prompts will not pop up on failure |
skipAuth | boolean | If true, 401 errors etc. will not trigger authentication redirection (e.g., redirecting to the login page) |
NocoBase Resource API supports a shorthand resource:action format:
| Format | Description | Example |
|---|---|---|
collection:action | Single collection CRUD | users:list, users:get, users:create, posts:update |
collection.relation:action | Associated resources (requires passing the primary key via resourceOf or URL) | posts.comments:list |
Relative paths will be concatenated with the application's baseURL (usually /api); cross-origin requests must use a full URL, and the target service must be configured with CORS.
The return value is an Axios response object. Common fields include:
response.data: Response bodydata.data (array of records) + data.meta (pagination, etc.)data.dataconst { data } = await ctx.request({
url: 'users:list',
method: 'get',
params: { pageSize: 10, page: 1 },
});
const rows = Array.isArray(data?.data) ? data.data : [];
const meta = data?.meta; // Pagination and other info
const res = await ctx.request({
url: 'users:create',
method: 'post',
data: { nickname: 'John Doe', email: '[email protected]' },
});
const newRecord = res?.data?.data;
const res = await ctx.request({
url: 'users:list',
method: 'get',
params: {
pageSize: 20,
sort: ['-createdAt'],
filter: { status: 'active' },
},
});
const res = await ctx.request({
url: 'some:action',
method: 'get',
skipNotify: true, // Do not pop up global message on failure
});
// Or decide whether to skip based on error type
const res2 = await ctx.request({
url: 'some:action',
method: 'get',
skipNotify: (err) => err?.name === 'CanceledError',
});
When using a full URL to request other domains, the target service must be configured with CORS to allow the current application's origin. If the target interface requires its own token, it can be passed via headers:
const res = await ctx.request({
url: 'https://api.example.com/v1/data',
method: 'get',
});
const res2 = await ctx.request({
url: 'https://api.other.com/items',
method: 'get',
headers: {
Authorization: 'Bearer <target_service_token>',
},
});
const { data } = await ctx.request({
url: 'users:list',
method: 'get',
params: { pageSize: 5 },
});
const rows = Array.isArray(data?.data) ? data.data : [];
ctx.render([
'<div style="padding:12px">',
'<h4>' + ctx.t('User List') + '</h4>',
'<ul>',
...rows.map((r) => '<li>' + (r.nickname ?? r.username ?? '') + '</li>'),
'</ul>',
'</div>',
].join(''));
skipNotify: true to catch and handle it yourself.ctx.request)