# REST API
REST API 允许通过 API 端点访问 content-types 。Strapi 在创建内容类型时自动创建 API endpoints 。API parameters 可以在查询 API 端点以优化结果时使用。
✏️ NOTE
默认情况下,REST API 不会填充任何关系、媒体字段、组件或动态区域。使用 populate
parameter 来填充特定字段。
# 端点
对于每个内容类型,将自动生成以下端点:
Examples:
✏️ NOTE
Components 没有 API 端点。
# 请求
请求将响应作为对象返回,该对象通常包含以下键:
data
: 响应数据本身,可以是:- 单个条目,作为具有以下键的对象
id
(number)attributes
(object)meta
(object)
- 条目列表,作为对象数组
- 自定义响应
- 单个条目,作为具有以下键的对象
meta
(object): 有关分页、发布状态、可用区域设置等的信息。error
(object, optional): 有关请求引发的任何 error 的信息
✏️ NOTE
某些插件(包括用户和权限和上传)可能不遵循此响应格式。
# Get entries
返回与查询筛选器匹配的条目(请参阅 API 参数 文档)。
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
的条目。
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
创建一个条目并返回其值。
如果安装了 Internationalization (i18n) plugin,则可以使用 POST 请求对 REST API 来 创建本地化条目.
Example request
POST http://localhost:1337/api/restaurants
{
"data": {
"title": "Hello",
"relation": 2,
"relations": [2, 4],
"link": {
"id": 1,
"type": "abc"
}
}
}
Example response
{
"data": {
"id": 1,
"attributes": { … },
"meta": {}
},
"meta": {}
}
# Update an entry
按 id
部分更新条目并返回其值。
未在查询中发送的字段不会在数据库中更改。发送 null
值以清除字段。
Example request
PUT http://localhost:1337/api/restaurants/1
{
"data": {
"title": "Hello",
"relation": 2,
"relations": [2, 4],
}
}
Example response
{
"data": {
"id": 1,
"attributes": {},
"meta": {}
},
"meta": {}
}
✏️ NOTE
即使安装了 Internationalization (i18n) plugin,目前也无法 更新本地条目.
# Delete an entry
删除按 id
排列的条目并返回其值。
Example request
DELETE http://localhost:1337/api/restaurants/1
Example response
{
"data": {
"id": 1,
"attributes": {},
"meta": {}
},
"meta": {}
}
← 提供者 筛选、区域设置和发布状态 →