> For the complete documentation index, see [llms.txt](https://gitbook.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.com/docs/documentation/zh/chuang-jian-nei-rong/openapi/guides/using-openapi-proxy.md).

# 使用 OpenAPI 代理

GitBook 可以代理 **测试** 请求，让它们即使在 API 服务器不支持 CORS 时也能正常工作。

### 为什么会有这个功能

除非 API 服务器通过 CORS 标头明确允许，否则浏览器会阻止跨源请求。如果未配置 CORS， **测试** 请求会在浏览器中失败。代理会通过 GitBook 在服务器端转发这些请求，从而绕过这一限制。

### 为整个规范启用代理

在 OpenAPI 规范的根级别添加 `x-enable-proxy: true` 。

<pre class="language-yaml"><code class="lang-yaml">openapi: '3.0.3'
<strong>x-enable-proxy: true
</strong>info:
  title: 示例 API
  version: '1.0.0'
servers:
  - url: https://api.example.com
</code></pre>

### 为特定操作启用或禁用

在某个操作上添加 `x-enable-proxy` 。

<pre class="language-yaml"><code class="lang-yaml">openapi: '3.0.3'
info:
  title: 示例 API
  version: '1.0.0'
servers:
  - url: https://api.example.com
paths:
  /reports:
    get:
      summary: 报告列表
<strong>      x-enable-proxy: true
</strong>      responses:
        '200':
          description: 确定
    post:
      summary: 创建报告
<strong>      x-enable-proxy: false
</strong>      responses:
        '201':
          description: 已创建
</code></pre>

{% hint style="info" %}
操作级别的 `x-enable-proxy` 优先于根级别的值。
{% endhint %}

### 代理支持什么

GitBook 会转发所有 `HTTP` 方法（`GET`, `POST`, `PUT`, `DELETE`, `PATCH`）、标头、Cookie 和请求正文。

### 安全性

代理仅会将请求转发到规范的 `servers` 数组中列出的 URL。它不能用于访问任意 URL。

{% hint style="info" %}
请确保 `servers` 数组包含你想要测试的所有基础 URL。如果某个 URL 未列出， **测试** 发往该主机的请求将绕过代理。
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gitbook.com/docs/documentation/zh/chuang-jian-nei-rong/openapi/guides/using-openapi-proxy.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
