# web 请求上下文

## 功能

Web 服务运行时会向脚本注入 web 对象。web 对象由请求数据字段和响应控制函数组成，具体结构见下方表格及对应 API 页面。

如何开启 Web 服务请阅读： [Web 服务](/docs/net/web)

## 请求数据

| API | 说明 |
| ------ | ------ |
| [web.get](/docs/web/get) | 读取 URL 查询参数对象。 |
| [web.post](/docs/web/post) | 读取表单 POST 字段对象。 |
| [web.cookie](/docs/web/cookie) | 读取请求 Cookie 对象，也用于请求结束后写回 Cookie 状态。 |
| [web.session](/docs/web/session) | 读取并保存当前请求 Session 对象。 |
| [web.files](/docs/web/files) | 读取上传文件对象。 |
| [web.server](/docs/web/server) | 读取请求和服务器信息。 |
| [web.url](/docs/web/url) | 读取当前请求路径。 |
| [web.method](/docs/web/method) | 读取当前 HTTP 请求方法。 |

## 请求对象概览

| API | 类型 | 结构 |
| ------ | ------ | ------ |
| web.get | Object | 查询参数名 -> String。 |
| web.post | Object | 表单字段名 -> String/Array。 |
| web.cookie | Object | Cookie 名称 -> String，并在请求结束时写回响应 Cookie。 |
| web.session | Object | Session 键名 -> JSON 可序列化值。 |
| web.files | Object | 上传字段名 -> 文件信息对象数组；文件信息字段见 [web.files](/docs/web/files)。 |
| web.server | Object | method、version、scheme、headers、local_addr、remote_addr、ip、port。 |

## 响应控制

| API | 说明 |
| ------ | ------ |
| [web.header](/docs/web/header) | 设置一个或一组响应头。 |
| [web.status_code](/docs/web/status_code) | 设置 HTTP 响应状态码。 |
| [web.redirect](/docs/web/redirect) | 设置 HTTP 重定向地址。 |

## 示例

```bt
name = web.get.name
web.header('Content-Type', 'text/plain; charset=utf-8')

// 输出：BT
echo(name)
```

## 注意事项

- web 对象只在 Web 请求执行期间存在。
- 响应控制状态会在脚本执行结束后统一写入 HTTP 响应。
