重寫外掛程式

重寫要求。

開發人員 Proxy 重寫傳入 API 要求的命令提示字元螢幕快照。

設定範例

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rc.schema.json",
  "plugins": [
    {
      "name": "RewritePlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "rewritePlugin"
    }
  ],
  "rewritePlugin": {
    "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rewriteplugin.schema.json",
    "rewritesFile": "rewrites.json"
  }
}

設定屬性

屬性 說明 預設
rewritesFile 包含重寫定義的檔案路徑 rewrites.json

命令列選項

無

重寫檔案範例

以下是重寫規則的範例。

重寫所有從 HTTP 到 HTTPS 的要求

將 HTTP 的所有要求重寫為 HTTPS。 在此內容中, 所有要求都 表示所有設定為 Dev Proxy 或 RewritePlugin 的要求。

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rewriteplugin.rewritesfile.schema.json",
  "rewrites": [
    {
      "in": {
        "url": "^http://(.*)"
      },
      "out": {
        "url": "https://$1"
      }
    }
  ]
}

設定請求標頭

將所有請求的標頭設 Accept-Language 為 https://api.contoso.com。 如果請求已經有標頭,外掛會替換其值。 如果標頭不見,外掛會自動補上。 因為規則沒有指定 出信 網址,外掛不會更改請求網址。

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rewriteplugin.rewritesfile.schema.json",
  "rewrites": [
    {
      "in": {
        "url": "^https://api\\.contoso\\.com/"
      },
      "out": {
        "headers": [
          {
            "name": "Accept-Language",
            "value": "nl-NL"
          }
        ]
      }
    }
  ]
}

從環境變數重寫請求到 URL

將 的 https://api.contoso.com 請求重寫為儲存在環境變數中的 API_BASE_URL 基礎 URL,保留其餘路徑。

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rewriteplugin.rewritesfile.schema.json",
  "rewrites": [
    {
      "in": {
        "url": "^https://api\\.contoso\\.com/(.*)"
      },
      "out": {
        "url": "${env:API_BASE_URL}/$1"
      }
    }
  ]
}

模擬檔案屬性

屬性 說明 必要
rewrites 重寫物件的陣列,這些物件會定義 RewritePlugin 套用至其攔截之要求的重寫規則清單 是

重寫物件

每個重寫規則都有下列屬性:

屬性 說明 必要
in 重寫模式 以符合連入要求。 是
out 重寫模式 以重寫要求 是

備註

如果 RewritePlugin 攔截的要求不符合模式中定義的所有屬性,外掛程式就不會將重寫規則套用至要求。

重寫模式

每個重寫模式都有下列屬性:

屬性 說明 必要 預設值 範例值
url 在 in 模式中,外掛套用於 URL 的正則表達式。 在 輸出 模式中,請重寫請求的 URL。 如果你在 輸出 模式中省略了它,外掛就不會改變請求網址。 是的,進 進,不行, 進出 ^http://(.*)
headers 請求 標頭 陣列,用於請求時設定。 只支援 出線 模式。 不

備註

出場模式必須指定 url、 或headers兩者兼具。

如果您在模式中的 正則表示式中使用擷取群組,您可以在 out 模式中參考它們。 例如,如果您想要重寫 http://example.com/foo 為 https://example.com/foo,您可以使用下列重寫規則:

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rewriteplugin.rewritesfile.schema.json",
  "rewrites": [
    {
      "in": {
        "url": "^http://(.*)"
      },
      "out": {
        "url": "https://$1"
      }
    }
  ]
}

在 out URL 中,你可以用語 ${env:VAR_NAME} 法來引用環境變數。 開發代理會插入環境變數的值 as-is,因此該值也可以指向 in 模式中的擷取群組。 Dev Proxy 只會解析符合 in 模式的請求,且不會擴展 in 模式中的環境變數。 如果沒有設定參考的環境變數,Dev Proxy 會記錄錯誤並跳過重寫規則。

請求標頭

每個請求標頭具有以下屬性:

屬性 說明 必要
name 請求標頭名稱 是
value 請求標頭要設定的值 是

備註

該外掛會不區分大小寫地匹配標頭名稱。 如果請求已經有標頭,外掛會替換其值。 如果標頭不見,外掛會自動補上。

該外掛僅在 URL 與 In URL 模式相符的請求上設定標頭。 外掛會在套用同一規則的 URL 重寫前評估匹配結果,因此當規則同時重寫 URL 和標頭時,標頭會套用到與 URL 重寫相同的請求。

你可以用語 ${env:VAR_NAME} 法在標頭值中引用環境變數。 Dev Proxy 會用和 out URL 一樣的方式解析它們,如果沒有設定參考的環境變數,則跳過重寫規則。 標頭值不支援 從 in 模式中擷取群組。