Skip to main content

REST API

REST API 允许通过 API 端点访问 content-types。创建内容类型时,Strapi 会自动创建 API 端点。查询 API 端点时可以使用 接口参数 来细化结果。

¥The REST API allows accessing the content-types through API endpoints. Strapi automatically creates API endpoints when a content-type is created. API parameters can be used when querying API endpoints to refine the results.

提醒

默认情况下,所有内容类型都是私有的,需要公开或需要使用适当的权限对查询进行身份验证。有关更多详细信息,请参阅 快速入门指南用户和权限插件API 令牌配置文档 的用户指南。

¥All content types are private by default and need to be either made public or queries need to be authenticated with the proper permissions. See the Quick Start Guide, the user guide for the Users & Permissions plugin, and API tokens configuration documentation for more details.

✏️ 注意

默认情况下,REST API 响应仅包含顶层字段,不会填充任何关系、媒体字段、组件或动态区域。使用 populate 参数 填充特定字段。确保为你填充的关系的字段授予查找权限。

¥By default, the REST API responses only include top-level fields and does not populate any relations, media fields, components, or dynamic zones. Use the populate parameter to populate specific fields. Ensure that the find permission is given to the field(s) for the relation(s) you populate.

🤓 上传插件 API

上传插件(处理 媒体库 中找到的媒体)具有 上传插件文档 中描述的特定 API。

¥The Upload plugin (which handles media found in the Media Library) has a specific API described in the Upload plugin documentation.

端点

¥Endpoints

对于每个 Content-Type,会自动生成以下端点:

¥For each Content-Type, the following endpoints are automatically generated:

方法网址描述
GET/api/:pluralApiId获取条目列表
POST/api/:pluralApiId创建条目
GET/api/:pluralApiId/:documentId获取入口
PUT/api/:pluralApiId/:documentId更新条目
DELETE/api/:pluralApiId/:documentId删除条目
Examples:

Restaurant 内容类型

¥Restaurant Content type

方法网址描述
得到/api/restaurants获取餐厅列表
邮政/api/restaurants创建一家餐厅
得到/api/restaurants/:id获取特定餐厅
删除/api/restaurants/:id删除餐厅
/api/restaurants/:id更新餐厅
✏️ 注意

组件 没有 API 端点。

¥Components don't have API endpoints.

💡 提示

默认情况下,API 端点以 /api 为前缀。这可以通过为 rest.prefix 配置参数设置不同的值来更改(请参阅 API 调用配置)。

¥API endpoints are prefixed with /api by default. This can be changed by setting a different value for the rest.prefix configuration parameter (see API calls configuration).

要求

¥Requests

请求以对象形式返回响应,通常包含以下键:

¥Requests return a response as an object which usually includes the following keys:

  • data:响应数据本身,可以是:

    ¥data: the response data itself, which could be:

    • 单个条目,作为具有以下键的对象:

      ¥a single entry, as an object with the following keys:

      • id(数字)

        ¥id (number)

      • attributes(目的)

        ¥attributes (object)

      • meta(目的)

        ¥meta (object)

    • 条目列表,作为对象数组

      ¥a list of entries, as an array of objects

    • 自定义响应

      ¥a custom response

  • meta(对象):有关分页、发布状态、可用区域设置等的信息。

    ¥meta (object): information about pagination, publication state, available locales, etc.

  • error(对象,可选):有关请求抛出的任何 error 的信息

    ¥error (object, optional): information about any error thrown by the request

✏️ 注意

某些插件(包括用户和权限以及上传)可能不遵循此响应格式。

¥Some plugins (including Users & Permissions and Upload) may not follow this response format.

获取条目

¥Get entries

返回与查询过滤器匹配的条目(请参阅 接口参数 文档)。

¥Returns entries matching the query filters (see API parameters documentation).

Example request

GET http://localhost:1337/api/restaurants

Example response
{
"data": [
{
"id": 1,
"attributes": {
"title": "Restaurant A",
"description": "Restaurant A's description"
},
"meta": {
"availableLocales": []
}
},
{
"id": 2,
"attributes": {
"title": "Restaurant B",
"description": "Restaurant B's description"
},
"meta": {
"availableLocales": []
}
},
],
"meta": {}
}

获取入口

¥Get an entry

返回 id 的条目。

¥Returns an entry by id.

Example request

GET http://localhost:1337/api/restaurants/1

Example response
{
"data": {
"id": 1,
"attributes": {
"title": "Restaurant A",
"description": "Restaurant A's description"
},
"meta": {
"availableLocales": []
}
},
"meta": {}
}

创建条目

¥Create an entry

创建一个条目并返回其值。

¥Creates an entry and returns its value.

如果安装了 国际化 (i18n) 插件,则可以使用对 创建本地化条目 的 REST API 的 POST 请求。

¥If the Internationalization (i18n) plugin is installed, it's possible to use POST requests to the REST API to create localized entries.

✏️ 注意

创建条目时,你可以定义其关系及其顺序(更多详细信息请参阅 通过 REST API 管理关系)。

¥While creating an entry, you can define its relations and their order (see Managing relations through the REST API for more details).

Example request

POST http://localhost:1337/api/restaurants

{
"data": {
"title": "Hello",
"relation_field_a": 2,
"relation_field_b": [2, 4],
"link": {
"id": 1,
"type": "abc"
}
}
}
Example response
{
"data": {
"id": 1,
"attributes": {},
"meta": {}
},
"meta": {}
}

更新条目

¥Update an entry

id 部分更新条目并返回其值。

¥Partially updates an entry by id and returns its value.

查询中未发送的字段不会在数据库中更改。发送 null 值以清除字段。

¥Fields that aren't sent in the query are not changed in the database. Send a null value to clear fields.

✏️ NOTES
Example request

PUT http://localhost:1337/api/restaurants/1

{
"data": {
"title": "Hello",
"relation_field_a": 2,
"relation_field_b": [2, 4],
}
}
Example response
{
"data": {
"id": 1,
"attributes": {},
"meta": {}
},
"meta": {}
}

删除条目

¥Delete an entry

删除 id 处的条目并返回其值。

¥Deletes an entry by id and returns its value.

Example request

DELETE http://localhost:1337/api/restaurants/1

Example response
{
"data": {
"id": 1,
"attributes": {},
"meta": {}
},
"meta": {}
}