我正在尝试使用 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"
我将 PUT 请求发送到https://api.contentful.com/spaces/{spaceID}/environments/master/entries/{postID}
Authorization 标头包含我之前生成的个人访问令牌。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 属性字段,包括 sys.id。
...所以,我想,我做的一切都是正确的——发帖、编辑数据和发回更新的帖子。sys
我尝试编辑我的 JSON 数据以在没有字段的情况下发送它,但没有运气。我被卡住了,有人知道我应该怎么做吗?