json - 通过 Postman 使用 PUT 请求更新 Contentful 帖子

标签 json postman contentful contentful-management

我正在尝试使用 Postman 更新 Contentful 条目。

我做了什么:

  • 在 Contentful 空间中,我创建了一个测试帖子来玩。
  • 转到设置 - API key - 内容管理 token 并生成个人访问 token
  • 在 Postman 中创建 GET 请求,传递空间 ID、主环境和测试帖子的 ID:

https://cdn.contentful.com/spaces/{spaceID}i/environments/master/entries?sys.id={postID} 我还发送了带有内容交付 token 的授权 header 。

enter image description here

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": "测试入口""name": "测试条目 123"

我将 PUT 请求发送到 https://api.contentful.com/spaces/{spaceID}/environments/master/entries/{postID}

enter image description here

授权 header 包含我之前生成的个人访问 token 。 X-Contentful-Version header 包含帖子的版本,可以在帖子详细信息中找到

enter image description here

当我发送此请求时,我收到带有空“字段”的 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 区域,帖子的所有字段都变为空。

enter image description here

内容丰富的文档说:

Contentful doesn't merge changes made to content, so when updating content, you need to send the entire body of an entry. If you update content with a subset of properties, you will lose all existing properties not included in that update.

You should always update resources in the following order:

  • Fetch current resource.
  • Make changes to the current resource.
  • Update the resource by passing the changed resource along with current version number.

This way no unseen changes are overridden and unexpected conflicts are unlikely to occur.

Note: You can't update any of the sys property fields, including sys.id.

...所以,我想,我做的一切都是对的——接受帖子、编辑数据并发回更新后的帖子。 我尝试编辑我的 JSON 数据以在没有 sys 字段的情况下发送它,但没有成功。 我被卡住了,有人知道我应该继续做什么吗?

最佳答案

感谢@whitep4nth3r 我能够解决这个问题。 我需要从我试图将其放入的同一来源获取数据。 授权 header 需要替换为用于 PUT 请求的个人访问 token 。

enter image description here

关于json - 通过 Postman 使用 PUT 请求更新 Contentful 帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69927426/

相关文章:

php - 如何从 PHP 中的 JSON.stringify 中删除反斜杠?

json - POSTMAN - 从 JSON 响应中保存属性值

c# - JSON 无法将 JSON 数组反序列化为类型

ajax - 将 .ajax() 与 JSONP 一起使用的基本示例?

json - 为什么 postman 中显示 "The field is required",即使它可以为空?

.net - WebApi/.net-core : Can't access to webapi

ruby-on-rails - 在功能规范中 stub Contentful Model实例的好方法是什么?

graph - Gatsby useStaticQuery 与语言环境

android - 如何从 Android 中 Contentful 的空间中检索条目

json - 如何合并由逗号分隔的过滤器生成的 jq JSON 对象