【问题标题】:How to tell the webhook the 'unpublish()' order comes from the API, not the Contentful console?如何告诉 webhook 'unpublish()' 命令来自 API,而不是 Contentful 控制台?
【发布时间】:2026-01-09 13:35:02
【问题描述】:

我使用这个trick(感谢@Robban)通过 API 发布一个 Contentful 条目,而不触发 webhook。

但是,我不知道如何在不触发 webhook 的情况下通过 API 取消发布条目。

根据 Contentful 文档,要通过 API 取消发布条目,它是这样的:

client.getSpace('<space_id>')
  .then((space) => space.getEntry('<entry_id>'))
  .then((entry) => entry.unpublish())

由于 &lt;entry_id&gt; 是唯一的有效负载,我如何向 webhook 指示它不应像往常一样继续,因为它是一个 API 调用?

【问题讨论】:

    标签: contentful contentful-management


    【解决方案1】:

    不幸的是,直接从 API 调用或从 Web 应用调用之间没有区别。 Web 应用程序在后台执行此调用。

    此外,在取消发布的情况下,您的 webhook 唯一会收到的是不包含任何字段的删除对象。这意味着上一个答案中显示的技巧在这里不适用。

    我能想到的解决这个问题的唯一方法是再次调用某个数据存储(可能是 Contentful),并在其中放置条目 id,也许还有一些时间戳。然后,您的 webhook 可以在收到取消发布事件后查询此数据存储,并查看处理是否应该继续,或者取消发布似乎是通过网络应用程序进行的。

    基本上是这样的:

    client.getSpace('<space_id>')
    .then((space) => space.getEntry('<entry_id>'))
    .then((entry) => {
    
             otherService.SetUnpublishedThroughManualAPICall(entry.sys.id);
             entry.unpublish();
    
          })
    

    然后在你的 webhook 中加入一些伪代码:

    function HandleUnpublish(object entry) {
    
        if(OtherService.CheckIfManualUnpublish(entry.sys.id)){
             //Do some processing...
        }
    }
    

    您可以选择使用 Contentful 中的字段作为您的商店。在这种情况下,您将在取消发布之前设置此字段。像这样的:

    client.getSpace('<space_id>')
    .then((space) => space.getEntry('<entry_id>'))
    .then((entry) => { 
         entry.fields['en-US'].unpublishedTroughApi = true;
         entry.update();
     })
    .then((entry) => entry.unpublish())
    

    然后在您的 webhook 中,您必须通过管理 API 再次获取条目并检查该字段。请记住,这会导致对 Contentful 进行大量额外的 API 调用。

    【讨论】:

      最近更新 更多