コンテンツにスキップ

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:

  1. Use the List zone rulesets operation to check if there is already a ruleset for the http_response_headers_transform phase at the zone level.

  2. 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
  3. 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レスポンスヘッダーを追加する

次の例は、既存のフェーズルールセット({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": []
}

必要なAPIトークンの権限

HTTPレスポンスヘッダー修正ルールを管理するためにAPIリクエストで使用されるAPIトークンは、少なくとも次の権限を持っている必要があります。

  • Transform Rules > Edit
  • Account Rulesets > Read