【发布时间】:2021-12-23 21:18:00
【问题描述】:
我正在尝试使用 Postman 更新 Contentful 条目。
我做了什么:
- 在 Contentful 空间中,我创建了一个测试帖子来玩。
- 转到设置 - API 密钥 - 内容管理令牌 并生成个人访问令牌
- 在 Postman 中创建了一个 GET 请求,传递了空间 ID、主环境和测试帖子的 ID:
https://cdn.contentful.com/spaces/{spaceID}i/environments/master/entries?sys.id={postID} 我还发送了带有内容传递令牌的授权标头。
GET 请求成功,我可以复制 JSON 对象响应。
{
"sys": {
"type": "Array"
},
"total": 1,
"skip": 0,
"limit": 100,
"items": [
{
"metadata": {
"tags": []
},
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "d9r4mg123x4v"
}
},
"id": "2Fwow39hxxx1bvMkjpsyV9",
"type": "Entry",
"createdAt": "2021-11-10T14:00:11.935Z",
"updatedAt": "2021-11-10T14:06:51.393Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 3,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "hotelInfo"
}
},
"locale": "en-US"
},
"fields": {
"name": "Test entry",
"slug": "test-entry",
"address": "Lviv",
"cityName": "Lviv",
"phone": "+380931231212",
"coordinates": {
"lon": -115.302,
"lat": 36.18709
},
"dog": "100",
"cat": "100",
"delivery": "100",
"photo": [
{
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "2hSnYhQDJzU99NvlsYdk3k"
}
}
],
"additionalInfo": {
"data": {},
"content": [
{
"data": {},
"content": [
{
"data": {},
"marks": [],
"value": "Test",
"nodeType": "text"
}
],
"nodeType": "paragraph"
}
],
"nodeType": "document"
},
"featuredHotel": true,
"phoneClicks": 1
}
}
],
"includes": {
"Asset": [
{
"metadata": {
"tags": []
},
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "d9r4mg123x4v"
}
},
"id": "2hSnYhQDJzU99NvlsYdk3k",
"type": "Asset",
"createdAt": "2021-11-10T13:59:59.954Z",
"updatedAt": "2021-11-10T13:59:59.954Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "JS",
"description": "Lorem Ipsum",
"file": {
"url": "//images.ctfassets.net/d9r4mg123x4v/2hSnYhQDJzU99NvlsYdk3k/6fbabc7be7f4b28dc8b7deadd9892205/JS.png",
"details": {
"size": 23078,
"image": {
"width": 1024,
"height": 1024
}
},
"fileName": "JS.png",
"contentType": "image/png"
}
}
}
]
}
}
现在我想创建 PUT 请求以将更新的 JSON 发送到 Contentful。 我将收到的 JSON 粘贴为 GET 请求的响应。 我更改了其中一个值:
"name": "Test entry"
到
"name": "Test entry 123"
我向https://api.contentful.com/spaces/{spaceID}/environments/master/entries/{postID}发送PUT请求
授权标头包含我之前生成的个人访问令牌。 X-Contentful-Version 标头包含帖子的版本,可以在帖子详情中找到
当我发送此请求时,我收到带有空“字段”的 JSON 响应:{}
{
"metadata": {
"tags": []
},
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "d9r4mg123x4v"
}
},
"id": "2Fwow39hxxx1bvMkjpsyV9",
"type": "Entry",
"createdAt": "2021-11-10T13:57:10.882Z",
"updatedAt": "2021-11-11T10:58:39.480Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"publishedVersion": 13,
"publishedAt": "2021-11-10T14:06:51.393Z",
"firstPublishedAt": "2021-11-10T14:00:11.935Z",
"createdBy": {
"sys": {
"type": "Link",
"linkType": "User",
"id": "4123123123zOn3MkhuVB"
}
},
"updatedBy": {
"sys": {
"type": "Link",
"linkType": "User",
"id": "4123123123zOn3MkhuVB"
}
},
"publishedCounter": 3,
"version": 23,
"publishedBy": {
"sys": {
"type": "Link",
"linkType": "User",
"id": "4123123123zOn3MkhuVB"
}
},
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "hotelInfo"
}
}
},
"fields": {}
}
在 Contentful Admin 区域,帖子的所有字段都变为空。
内容丰富的文档说:
Contentful 不会合并对内容所做的更改,因此在更新时 内容,您需要发送条目的整个正文。如果你更新 包含属性子集的内容,您将丢失所有现有的 该更新中未包含的属性。
您应该始终按以下顺序更新资源:
- 获取当前资源。
- 对当前资源进行更改。
- 通过将更改的资源与当前版本号一起传递来更新资源。
这种方式不会覆盖看不见的更改,并且不会发生意外冲突 不太可能发生。
注意:您不能更新任何 sys 属性字段,包括 系统ID。
...所以,我想,我做的一切都是正确的——发帖、编辑数据和发回更新的帖子。
我尝试编辑我的 JSON 数据以在没有 sys 字段的情况下发送它,但没有运气。
我被卡住了,有人知道我应该怎么做吗?
【问题讨论】:
-
这里有内容的 DevRel!非常感谢您的详细解释。我想知道 CDA 和 CMA 上的有效载荷是否不同。也许尝试从 CMA (api.contentful.com) 获取初始负载并将其重新发送回相同的 API URL?
-
@whitep4nth3r 对!是的,我认为我必须使用 CDA 并避免使用 CMA 来获取数据。
标签: json postman contentful contentful-management