Version: 1.0.0
MySQL RESTful API
功能介绍
这里给出了调用 mysql 所有操作的API,以及每个API的参数参考。
请求域名
支持以下三种域名访问方式:
https://{envId}.api.tcloudbasegateway.com/v1/rdb/rest/{table}https://{envId}.api.tcloudbasegateway.com/v1/rdb/rest/{schema}/{table}https://{envId}.api.tcloudbasegateway.com/v1/rdb/rest/{instance}/{schema}/{table}
- envId 是环境id
- instance 是数据库实例标识
- schema 是数据库名
- table 是表名
如果使用系统数据库,推荐方式 1
接入指引
调用以下接口需要传递 AccessToken,格式如Authorization: Bearer <token>。Token 获取方式参考:https://docs.cloudbase.net/http-api/basic/access-token
请求编码
当发起请求前请进行 url 编码,如:
原始请求
curl -i -X GET 'https://{{host}}/v1/rdb/rest/course?select=name,position&name=like.%张三%&title=eq.文章标题'编码后请求
curl -i -X GET 'https://{{host}}/v1/rdb/rest/course?select=name,position&name=like.%%E5%BC%A0%E4%B8%89%&title=eq.%E6%96%87%E7%AB%A0%E6%A0%87%E9%A2%98'
请求头和返回头
| 请求头 | 参数 | 说明 | 示例 |
| Accept | 支持 `application/json`、`application/vnd.pgrst.object+json` | 控制数据返回格式 | Accept: application/json |
| Content-Type | 支持 `application/json`、`application/vnd.pgrst.object+json` | 返回 | Content-Type: application/json |
| X-EnvId | 环境id | X-EnvId: tcb-xxx | |
| X-Db-Instance | 数据库实例标识 | 系统默认数据库标识 default,自定义实例标识可在云开发控制台查看 | X-Db-Instance: default |
| Content-Profile | 写操作数据库名 | 指定数据库名,系统默认数据库名使用环境id | Content-Profile: tcb-xxx |
| Accept-Profile | 读操作数据库名 | 指定数据库名,系统默认数据库名使用环境id | Accept-Type: tcb-xxx |
| Prefer | 操作依赖的特征值 | - `return=representation` 写操作,返回数据体和数据头 - `return=minimal` 写操作,不返回数据体只返回头,写操作默认类型 - `count=exact` 读操作,指定 count 计数 - `resolution=merge-duplicates` upsert操作,合并冲突项 - `resolution=ignore-duplicates` upsert操作,忽略冲突项 | Prefer: return=representation |
| Preference-Applied | 使用请求时Prefer的类型 | 返回信息使用请求时的Prefer,会将对应的特征信息以此返回,某些情况下会有默认值 | Preference-Applied: return=representation |
| Content-Range | 分页信息 | - 当查询时指定 Prefer: count=exact 时会返回分页信息入 0-1/2,其中0表示起始位,1表示偏移量,2表示总数 (注意,此处的起始位、偏移量并非 mysql 中的 offset、limit关键字含义,而是指查询后的数据位置,通常配合 limit 使用) - 当写操作时指定 return=representation 或 return=minimal (默认) 时,会返回分页信息,如 *-*/2,其中2表示受影响行数 | Content-Range: 0-1/2 |
HTTP 状态码与错误码
| 错误码 | HTTP状态码 | 说明 |
| InternalError.SYS_ERR | 500 | 系统内部错误 |
| InvalidParameter.INVALID_PARAM | 400 | 请求参数无效 |
| AuthFailure.DataSourceOperationAuthFailure | 403 | 鉴权失败,请在权限模块设置合适的权限 |
| AuthFailure.SetOwnerNotAllowed | 400 | 不允许设置用户标识字段 |
| FailedOperation.EmptyDatabaseEndpoint | 503 | 数据库端点为空 |
| FailedOperation.DatabaseConnectError | 503 | 建立数据库连接失败 |
| ResourceNotFound.InstanceNotFound | 404 | 未找到数据库实例 |
| ResourceNotFound.TableNotFound | 404 | 未找到表 |
| ResourceNotFound.ColumnNotFound | 400 | 未找到列 |
| FailedOperation.TaskNotFound | 500 | 未找到任务 |
| FailedOperation.ConcurrentImportNowAllowed | 500 | 导入任务已存在,请等待当前导入任务完成 |
| FailedOperation.EmptyFile | 500 | 文件为空 |
| FailedOperation.FileContainsZeroDataRows | 500 | 文件不包含任何数据行 |
| FailedOperation.InvalidFileContent | 500 | 文件内容无效 |
| FailedOperation.DownloadFileFailed | 500 | 下载文件失败 |
| UnsupportedOperation.TooManyTables | 503 | 表数量超过限制 |
| FailedOperation.BadApiRequest | 400 | 无效请求 |
| FailedOperation.TdsqlPaused | 503 | 实例正在恢复中,请稍后重试连接 |
| FailedOperation.DatabaseExecSqlError | 400 | 执行SQL错误 |
| FailedOperation.DatabaseSchemaError | 400 | 数据库架构错误 |
| InvalidParameter.ResponseUnacceptableSingleError | 406 | 响应不符合单条记录要求 |
| FailedOperation.DatabaseBuildSqlError | 500 | 构建SQL错误 |
| FailedOperation.DatabaseDataProcessError | 500 | 数据处理错误 |
| AuthFailure.AuthError | 401 | 权限错误 |
注意点
- 所有 POST、PATCH、DELETE 一样,请求头带
Prefer: return=representation表示有返回体,不带表示无回包只有返回头 - POST、PATCH、DELETE 的返回体通常是
[]json array,如果请求头指定了Accept: application/vnd.pgrst.object+json,那么会返回{}json object - 如果指定
Accept: application/vnd.pgrst.object+json,但是数据是大于 1 的量,则会报错