POST编码
把文本或字节转换为 Base64。标准或 URL 安全字母表,带或不带填充,可选换行。
POST
https://api.base64hub.com/v1/encode请求
发送 JSON,把文本放在 input 中。文本按 UTF-8 编码。
curl -s https://api.base64hub.com/v1/encode \
-H 'Content-Type: application/json' \
-d '{"input": "Hello, hub!"}'
const res = await fetch("https://api.base64hub.com/v1/encode", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ input: "Hello, hub!" }),
});
const { output } = await res.json();
console.log(output); // SGVsbG8sIGh1YiE=
import json
import urllib.request
req = urllib.request.Request(
"https://api.base64hub.com/v1/encode",
data=json.dumps({"input": "Hello, hub!"}).encode(),
headers={"Content-Type": "application/json"},
)
with urllib.request.urlopen(req) as res:
print(json.load(res)["output"]) # SGVsbG8sIGh1YiE=
b64 "Hello, hub!"
# SGVsbG8sIGh1YiE=
请求体参数
| 名称 | 类型 | 说明 |
|---|---|---|
input 必填 | string | 要编码的文本。会先转换为 UTF-8 字节。 |
variant 可选 | "standard" 或 "url" | 字母表。url 用 - 和 _ 代替 + 和 /。默认为 "standard"。 |
padding 可选 | boolean | 保留末尾的 =。默认为 true。 |
wrap 可选 | integer,0 到 1000 | 每 N 个输出字符后插入 \n,结尾不加。76 对应 MIME。默认为 0,即不换行。 |
其他任何字段都会以 400 invalid_body 拒绝。
原始字节
要编码文件或任何二进制数据,请以 application/octet-stream 发送字节。此时选项放在查询字符串中:variant=url、padding=false、wrap=76。
printf '\xfb\xff\xbf' | curl -s \
'https://api.base64hub.com/v1/encode?variant=url&padding=false' \
-H 'Content-Type: application/octet-stream' \
--data-binary @-
const bytes = new Uint8Array([0xfb, 0xff, 0xbf]);
const res = await fetch(
"https://api.base64hub.com/v1/encode?variant=url&padding=false",
{ method: "POST", headers: { "Content-Type": "application/octet-stream" }, body: bytes },
);
console.log((await res.json()).output); // -_-_
import json
import urllib.request
req = urllib.request.Request(
"https://api.base64hub.com/v1/encode?variant=url&padding=false",
data=b"\xfb\xff\xbf",
headers={"Content-Type": "application/octet-stream"},
)
with urllib.request.urlopen(req) as res:
print(json.load(res)["output"]) # -_-_
printf '\xfb\xff\xbf' | b64 --url --no-pad
# -_-_
请求体上限为 1 MB。更大的文件请用支持流式处理的 CLI。
响应
200 OK · application/json
{
"output": "SGVsbG8sIGh1YiE=",
"variant": "standard",
"padding": true,
"bytes_in": 11,
"bytes_out": 16
}
| 字段 | 类型 | 说明 |
|---|---|---|
output | string | Base64 文本。 |
variant | string | 使用的字母表,standard 或 url。 |
padding | boolean | 是否保留了 = 填充。 |
bytes_in | integer | 转换为 UTF-8 后的输入字节数。 |
bytes_out | integer | output 的长度,包括换行符。 |
使用 {"input": "foobar", "wrap": 4} 时,输出为 "Zm9v\nYmFy",bytes_out 为 9:八个 Base64 字符加一个换行符。上面的响应为了便于阅读做了格式化;API 实际发送的是一行。
错误
| 状态码 | 代码 | 原因 |
|---|---|---|
| 400 | invalid_body | 不是有效的 JSON,缺少 input 或它不是字符串,variant、padding 或 wrap 的值错误,有未知的字段或查询参数,或者在 JSON 请求体中附带了查询选项。 |
| 413 | too_large | 请求体超过 1 MB。 |
| 415 | unsupported_media_type | Content-Type 不是 application/json 或 application/octet-stream。 |
| 429 | 无 | 达到速率限制;见 限制。 |
API 概览 列出了所有错误。
更新于 2026年11月22日