创建Agent后端和API
更新时间:2026-09-22
该接口用于为 Agent 服务一并创建专属 Agent 后端和已发布的 Agent API,并返回 backendId 和 apiId。
注意事项:
serviceId必须属于 Agent 服务。- 后端地址使用
host[:port]格式,不含协议;省略端口时使用 80。 - 创建的 API 固定绑定本次创建的单个 Agent 后端,创建成功后路由即生效。
请求结构
Plain Text
1POST /?action=CreateAgentBackendAndApi HTTP/1.1
2Host: apigw-control.bj.baidubce.com
3Authorization: authorization string
4
5{
6 "instanceId": instanceId,
7 "serviceId": serviceId,
8 "backend": {
9 "backendServiceEndpoint": backendServiceEndpoint
10 },
11 "apiType": apiType,
12 "methods" : [method],
13 "path": path,
14 "matchType": matchType,
15 "urlRewritePath": urlRewritePath,
16 "urlRewriteType": urlRewriteType,
17 "urlRewriteRegexMatch": urlRewriteRegexMatch,
18 "urlRewriteRegexRewrite": urlRewriteRegexRewrite
19}
请求头域
除公共头域外,无其它特殊头域。
请求参数
| 参数名称 | 类型 | 是否必需 | 参数位置 | 描述 |
|---|---|---|---|---|
| instanceId | String | 是 | RequestBody参数 | 网关实例 ID |
| serviceId | String | 是 | RequestBody参数 | 服务 ID,必须是 Agent 服务 |
| backend | AgentBackend | 是 | RequestBody参数 | |
| apiType | String | 是 | RequestBody参数 | API 类型 |
| methods | List<String> | 是 | RequestBody参数 | 请求方法 |
| path | String | 是 | RequestBody参数 | 路径 |
| matchType | String | 是 | RequestBody参数 | 匹配类型 |
| urlRewritePath | String | 否 | RequestBody参数 | 重写后的请求路径;为空时不开启路径重写 |
| urlRewriteType | String | 否 | RequestBody参数 | 路径重写匹配类型:Exact / Prefix / Regex;不传时按匹配类型推导 |
| urlRewriteRegexMatch | String | 否 | RequestBody参数 | 正则重写模式,如 ^/user/(/+)(/.*)$;重写类型为 Regex 时必填 |
| urlRewriteRegexRewrite | String | 否 | RequestBody参数 | 正则重写替换表达式,如 /instance/\1;重写类型为 Regex 时必填 |
返回头域
除公共头域,无其它特殊头域。
返回参数
| 参数名称 | 类型 | 描述 |
|---|---|---|
| backendId | String | 后端 ID |
| apiId | String | API ID |
错误码
| 错误码 | 错误描述 | HTTP状态码 | 中文解释 |
|---|---|---|---|
| MethodArgumentNotValidException | MethodArgumentNotValidException: serviceId must not be blank | 400 | 必填参数缺失或格式不合法,serviceId 不能为空 |
| HttpMessageNotReadable | Http Message Not Readable: JSON parse error | 400 | 请求体不是合法 JSON,无法解析,请检查 JSON 语法和字段类型 |
| ResourcePermissionDeny | Resource permission deny. | 401 | 无权限访问指定资源,请确认实例属于当前账号并使用有权限的账号凭证 |
| ServiceNotExist | Service not exist. | 404 | serviceId 对应的服务不存在、不属于当前账号或已删除 |
| HttpMediaTypeException | HttpMediaTypeException: Content-Type 'text/plain' is not supported | 415 | 请求的 Content-Type 不受支持,请使用 application/json |
| DataPlaneSyncFailed | 配置已保存成功,但网关操作失败,请稍后重试或联系技术支持。 | 500 | 配置已写入控制面,但下发数据面失败,请稍后重试或联系技术支持 |
请求示例
Plain Text
1POST /?action=CreateAgentBackendAndApi HTTP/1.1
2Host: apigw-control.bj.baidubce.com
3Authorization: bce-auth-v1/f81d3b34e48048fbb2634dc7882d7e21/2015-08-11T04:17:29Z/3600/host/74c506f68c65e26c633bfa104c863fffac5190fdec1ec24b7c03eb5d67d2e1de
4
5{
6 "instanceId": "gw-a1b2c3d4",
7 "serviceId": "sg-a1b2c3d4",
8 "apiType": "http",
9 "backend": {
10 "backendServiceEndpoint": "agent.example.com:8080"
11 },
12 "methods": [
13 "POST"
14 ],
15 "path": "/v1/agent",
16 "matchType": "Prefix"
17}
返回示例
Plain Text
1HTTP/1.1 200 OK
2x-bce-request-id: 1214cca7-4ad5-451d-9215-71cb844c0a50
3Date: Wed, 03 Dec 2014 06:42:19 GMT
4Content-Type: application/json;charset=UTF-8
5Server: BWS
6
7{
8 "backendId": "bd-a1b2c3d4",
9 "apiId": "api-a1b2c3d4"
10}
评价此篇文章
