# 更新表单 (PUT)

替换表单的字段、设计和设置。所有字段都会被应用，因此请发送完整的表单，而不是部分更新。

## 端点

```
PUT https://api.pocketalert.app/v1/forms/{tid}
```

## 身份验证

> **Info: 必填**
>
> 在请求中包含以下请求头之一：
> - `Token: <your-api-key>` — 从 [API 密钥](https://pocketalert.app/account/apikeys) 获取
> - `Authorization: Bearer <jwt-token>` — 来自 [登录](https://info.pocketalert.app/api/user/login.md)

## 请求

### 路径参数

| 参数 | 类型 | 必填 | 描述 |
|-----------|------|:--------:|-------------|
| `tid` | string | ✅ | 表单标识符 |

### 请求体参数

与[创建表单](https://info.pocketalert.app/api/forms/create.md#body-parameters)相同。未包含的参数都会重置为默认值：省略 `allowed_origins` 会清空列表，省略 `application_tid` 会解除与应用的关联。

> **Warning: 发送当前完整状态**
>
> 编辑器会先加载表单，修改需要的内容，然后将结果发回。如果客户端只发送修改过的键，其余内容会被悄悄清除。

### 请求示例

**cURL**

```bash
curl -X PUT "https://api.pocketalert.app/v1/forms/vml19aihrga216gt8apc5e3m9" \
  -H "Token: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Contact form",
    "is_active": false,
    "max_responses": 100,
    "allowed_origins": ["example.com", "*.shop.example.com"],
    "fields": [
      { "id": "name", "type": "text", "label": "Name", "required": true },
      { "id": "email", "type": "email", "label": "Email", "required": true }
    ],
    "settings": { "title": "Get in touch" }
  }'
```

**JavaScript**

```javascript
const tid = 'vml19aihrga216gt8apc5e3m9';

// Load the form, change one thing, send it all back.
const [form] = await fetch('https://api.pocketalert.app/v1/forms', {
  headers: { 'Token': 'your-api-key' }
}).then((r) => r.json()).then((forms) => forms.filter((f) => f.tid === tid));

await fetch(`https://api.pocketalert.app/v1/forms/${tid}`, {
  method: 'PUT',
  headers: {
    'Token': 'your-api-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ ...form, is_active: false })
});
```

**Python**

```python
import requests

tid = 'vml19aihrga216gt8apc5e3m9'
headers = {'Token': 'your-api-key'}

forms = requests.get('https://api.pocketalert.app/v1/forms', headers=headers).json()
form = next(f for f in forms if f['tid'] == tid)

requests.put(
    f'https://api.pocketalert.app/v1/forms/{tid}',
    headers=headers,
    json={**form, 'is_active': False}
)
```

## 响应

### 成功响应

> **Tip: 200 OK**
>
> 更新后的表单，结构与[创建表单](https://info.pocketalert.app/api/forms/create.md#response)中的一致。

### 错误响应

| 状态码 | 描述 |
|:------:|-------------|
| `400` | 验证错误，`error` 字段会说明具体问题 |
| `401` | 未授权，令牌无效或缺失 |
| `404` | 表单不存在，或属于其他账户 |
| `500` | 更新表单失败 |
