Skip to main content

Strapi 5 具有一种新的扁平化 REST API 调用响应格式

¥Strapi 5 has a new, flattened response format for REST API calls

在 Strapi 5 中,REST API 响应格式已简化和扁平化。你可以将 Strapi-Response-Format: v4 标头设置为使用旧的 v4 格式,同时转换代码以充分考虑新的 Strapi 5 响应格式。

¥In Strapi 5, the REST API response format has been simplified and flattened. You can set the Strapi-Response-Format: v4 header to use the old v4 format while you convert your code to fully take into account the new Strapi 5 response format.

此页面是 重大变更数据库 的一部分,提供有关重大更改的信息以及从 Strapi v4 迁移到 Strapi 5 的其他说明。

¥This page is part of the breaking changes database and provides information about the breaking change and additional instructions to migrate from Strapi v4 to Strapi 5.

\🔌 此重大更改是否会影响插件?\
\🤖 此重大更改是否由 codemod 自动处理?\

重大更改描述

¥Breaking change description

在 Strapi v4 中

¥In Strapi v4

Content API 返回封装在 attributes 参数内的请求内容的所有属性:

¥The Content API returns all the attributes of requested content wrapped inside an attributes parameter:

{
"data": {
// system fields
"id": 14,
"attributes": {
// user fields
"title": "Article A"
"relation": {
"data": {
"id": "2"
"name": "Category A"
}
}
}
}
"meta": {
"pagination": {
"page": 1,
"pageSize": 10
}
}
}

在 Strapi 5 中

¥In Strapi 5

Content API 返回请求内容的属性,而不将它们封装在属性对象中,并且使用 documentId 而不是 id

¥The Content API returns attributes of requested content without wrapping them in an attributes object, and a documentId is used instead of an id:

{
"data": {
// system fields
"documentId": "clkgylmcc000008lcdd868feh",
"locale": "en",
// user fields
"title": "Article A"
"relation": {
// system fields
"documentId": "clkgylw7d000108lc4rw1bb6s"
// user fields
"name": "Category A"
}
}
"meta": {
"pagination": {
"page": 1,
"pageSize": 10
}
}
}

迁移

¥Migration

注意

¥Notes

要使用 Strapi v4 响应格式,请设置以下标头:Strapi-Response-Format: v4

¥To use the Strapi v4 response format, set the following header: Strapi-Response-Format: v4.

手动程序

¥Manual procedure

确保你的 API 调用考虑到新的响应格式,或设置可选标头以继续使用 Strapi v4 响应格式(参见 notes)。

¥Ensure your API calls take into account the new response format, or set the optional header to keep on using the Strapi v4 response format (see notes).