通过 Cloudflare API 转发 URL

Forwading URL Through Cloudflare API

我正在尝试使用 Cloudflare 的 API 创建页面规则以将 http 转发到 https。不幸的是,我认为文档并未 100% 清楚地说明如何执行此操作。这是我目前传递给 POST 正文的 JSON 对象:

{
  "targets": [
    {
      "target": "url",
      "constraint": {
        "operator": "matches",
        "value": "http://exampletest.com/*"
      }
    }
],
  "actions": [{
    "id": "forwarding_url",
    "value": "https://exampletest.com/"
  }]
}

这是我要回复的消息:

{
"success": false,
"errors": [
    {
        "code": 1004,
        "message": "Page Rule validation failed: See messages for details."
    }
],
"messages": [
    {
        "code": 1,
        "message": ".settings[0].url: This value should not be blank.",
        "type": null
    },
    {
        "code": 2,
        "message": ".settings[0].statusCode: This value should not be blank.",
        "type": null
    }
],
"result": null

}

所以我似乎需要在某处设置一个设置对象,但无论我尝试添加设置的任何方式,我都会收到相同的消息。有谁知道我在这里做错了什么?这是 Cloudflare 关于该主题的文档。不确定我是否遗漏了什么:

https://api.cloudflare.com/#page-rules-for-a-zone-create-page-rule

实际上,只是想出了这个。看起来这是正确的格式:

{
  "targets": [
    {
      "target": "url",
      "constraint": {
        "operator": "matches",
        "value": "http://exampletest.com/*"
      }
    }
  ],
  "actions": [
    {
      "id": "forwarding_url",
      "value": {
        "url": "https://www.exampletest.com/",
        "status_code": 301
      }
    }
  ]
}

您必须设置一个参数 - "status":"active" 才能实际激活页面规则,否则您只是在创建一个非活动规则。

Cloudflare 将此设置为可选参数,但激活规则需要它。

这个答案的更新版本是这样的:

{
  "targets": [
    {
      "target": "url",
      "constraint": {
        "operator": "matches",
        "value": "http://exampletest.com/*"
      }
    }
  ],
  "actions": [
    {
      "id": "forwarding_url",
      "value": {
        "url": "https://www.exampletest.com/",
        "status_code": 301
      }
    }
  ],
  "status": "active"
}