通过Cloudflare API替换URL

时间:2018-11-11 22:28:25

标签: api cloudflare

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

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

这是我回来的消息:

{
"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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

Cloudflare将此设置为可选参数,但是激活该规则是必需的。

此答案的更新版本如下...

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