APIを介してルールを作成する
Rulesets APIを使用して、APIを介してHTTPレスポンスヘッダー修正ルールを作成します。一般的なユースケースについては、ルール例ギャラリーを参照してください。
APIを介してHTTPレスポンスヘッダー修正ルールを作成する際は、次のことを確認してください。
- ルールアクションを
rewriteに設定します。 - 実行する操作(ヘッダーの設定、追加、または削除)に応じて、
action_parametersフィールドにヘッダー修正パラメータを定義します。 - ルールをゾーンレベルの
http_response_headers_transformフェーズにデプロイします。
Follow this workflow to create HTTPレスポンスヘッダー修正ルール for a given zone via API:
-
Use the List zone rulesets operation to check if there is already a ruleset for the
http_response_headers_transformphase at the zone level. -
If the phase ruleset does not exist, create it using the Create a zone ruleset operation. In the new ruleset properties, set the following values:
- kind:
zone - phase:
http_response_headers_transform
- kind:
-
Use the Update a zone ruleset operation to add HTTPレスポンスヘッダー修正ルール to the list of ruleset rules. Alternatively, include the rule in the Create a zone ruleset request mentioned in the previous step.
APIトークンがAPI操作を実行するために必要な権限を持っていることを確認してください。
例: HTTPレスポンスヘッダーを静的値に設定する
次の例は、既存のフェーズルールセット({ruleset_id})のルールを単一のHTTPレスポンスヘッダー修正ルールに構成します。これは、ゾーンルールセットを更新する操作を使用して、HTTPレスポンスヘッダーを静的値に設定します。
curl --request PUT \https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '{ "rules": [ { "expression": "(starts_with(http.request.uri.path, \"/en/\"))", "description": "私の最初のHTTPレスポンスヘッダー修正ルール", "action": "rewrite", "action_parameters": { "headers": { "X-Source": { "operation": "set", "value": "Cloudflare" } } } } ]}'レスポンスには、更新したルールセットの完全な定義が含まれています。
{ "result": { "id": "<RULESET_ID>", "name": "ゾーンレベルレスポンスヘッダートランスフォームルールセット", "description": "レスポンスヘッダー修正ルールを実行するゾーンレベルのルールセット。", "kind": "zone", "version": "2", "rules": [ { "id": "<RULE_ID>", "version": "1", "action": "rewrite", "action_parameters": { "headers": { "X-Source": { "operation": "set", "value": "Cloudflare" } } }, "expression": "(starts_with(http.request.uri.path, \"/en/\"))", "description": "私の最初のHTTPレスポンスヘッダー修正ルール", "last_updated": "2021-04-14T14:42:04.219025Z", "ref": "<RULE_REF>" } ], "last_updated": "2021-04-14T14:42:04.219025Z", "phase": "http_response_headers_transform" }, "success": true, "errors": [], "messages": []}例: HTTPレスポンスヘッダーを動的値に設定する
次の例は、既存のフェーズルールセット({ruleset_id})のルールを単一のHTTPレスポンスヘッダー修正ルールに構成します。これは、ゾーンルールセットを更新する操作を使用して、HTTPレスポンスヘッダーを動的値に設定します。
curl --request PUT \https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '{ "rules": [ { "expression": "(starts_with(http.request.uri.path, \"/en/\"))", "description": "私の最初のHTTPレスポンスヘッダー修正ルール", "action": "rewrite", "action_parameters": { "headers": { "X-Bot-Score": { "operation": "set", "expression": "to_string(cf.bot_management.score)" } } } } ]}'レスポンスには、更新したルールセットの完全な定義が含まれています。
{ "result": { "id": "<RULESET_ID>", "name": "ゾーンレベルレスポンスヘッダートランスフォームルールセット", "description": "レスポンスヘッダー修正ルールを実行するゾーンレベルのルールセット。", "kind": "zone", "version": "2", "rules": [ { "id": "<RULE_ID>", "version": "1", "action": "rewrite", "action_parameters": { "headers": { "X-Bot-Score": { "operation": "set", "expression": "to_string(cf.bot_management.score)" } } }, "expression": "(starts_with(http.request.uri.path, \"/en/\"))", "description": "私の最初のHTTPレスポンスヘッダー修正ルール", "last_updated": "2021-04-14T14:42:04.219025Z", "ref": "<RULE_REF>" } ], "last_updated": "2021-04-14T14:42:04.219025Z", "phase": "http_response_headers_transform" }, "success": true, "errors": [], "messages": []}例: 静的値を持つset-cookie HTTPレスポンスヘッダーを追加する
set-cookie HTTPレスポンスヘッダーを追加する次の例は、既存のフェーズルールセット({ruleset_id})のルールを単一のHTTPレスポンスヘッダー修正ルールに構成します。これは、ゾーンルールセットを更新する操作を使用して、静的値を持つset-cookie HTTPレスポンスヘッダーを追加します。add操作でルールを構成することにより、レスポンスに既に存在する可能性のあるset-cookieヘッダーを保持します。
curl --request PUT \https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '{ "rules": [ { "expression": "(starts_with(http.request.uri.path, \"/en/\"))", "description": "私の最初のHTTPレスポンスヘッダー修正ルール", "action": "rewrite", "action_parameters": { "headers": { "set-cookie": { "operation": "add", "value": "mycookie=custom_value" } } } } ]}'レスポンスには、更新したルールセットの完全な定義が含まれています。
{ "result": { "id": "<RULESET_ID>", "name": "ゾーンレベルレスポンスヘッダートランスフォームルールセット", "description": "レスポンスヘッダー修正ルールを実行するゾーンレベルのルールセット。", "kind": "zone", "version": "2", "rules": [ { "id": "<RULE_ID>", "version": "1", "action": "rewrite", "action_parameters": { "headers": { "set-cookie": { "operation": "add", "value": "mycookie=custom_value" } } }, "expression": "(starts_with(http.request.uri.path, \"/en/\"))", "description": "私の最初のHTTPレスポンスヘッダー修正ルール", "last_updated": "2021-04-14T14:42:04.219025Z", "ref": "<RULE_REF>" } ], "last_updated": "2021-04-14T14:42:04.219025Z", "phase": "http_response_headers_transform" }, "success": true, "errors": [], "messages": []}例: HTTPレスポンスヘッダーを削除する
次の例は、既存のフェーズルールセット(<RULESET_ID>)のルールを単一のHTTPレスポンスヘッダー修正ルールに構成します。これは、ゾーンルールセットを更新する操作を使用して、HTTPレスポンスヘッダーを削除します。
curl --request PUT \https://api.cloudflare.com/client/v4/zones/{zone_id}/rulesets/{ruleset_id} \--header "Authorization: Bearer <API_TOKEN>" \--header "Content-Type: application/json" \--data '{ "rules": [ { "expression": "(starts_with(http.request.uri.path, \"/en/\"))", "description": "私の最初のHTTPレスポンスヘッダー修正ルール", "action": "rewrite", "action_parameters": { "headers": { "cf-connecting-ip": { "operation": "remove" } } } } ]}'レスポンスには、更新したルールセットの完全な定義が含まれています。
{ "result": { "id": "<RULESET_ID>", "name": "ゾーンレベルレスポンスヘッダートランスフォームルールセット", "description": "レスポンスヘッダー修正ルールを実行するゾーンレベルのルールセット。", "kind": "zone", "version": "2", "rules": [ { "id": "<RULE_ID>", "version": "1", "action": "rewrite", "action_parameters": { "headers": { "cf-connecting-ip": { "operation": "remove" } } }, "expression": "(starts_with(http.request.uri.path, \"/en/\"))", "description": "私の最初のHTTPレスポンスヘッダー修正ルール", "last_updated": "2021-04-14T14:42:04.219025Z", "ref": "<RULE_REF>" } ], "last_updated": "2021-04-14T14:42:04.219025Z", "phase": "http_response_headers_transform" }, "success": true, "errors": [], "messages": []}HTTPレスポンスヘッダー修正ルールを管理するためにAPIリクエストで使用されるAPIトークンは、少なくとも次の権限を持っている必要があります。
- Transform Rules > Edit
- Account Rulesets > Read