HTTP 请求
目前橙汁云函数内置了4种常见的
http
请求模式,支持GET
,POST
,PUT
,DELETE
。参数方面仅支持查询参数
、JSON请求体
、请求头
三个位置进行参数设置,橙汁云函数没有典型的httpResponse
操作,响应值会随着return
直接返回。
查询参数
只有当参数被提前设置时,才能在ctx.query
当中被获取
javascript
export default async function handler(ctx: CloudContext) {
const query = ctx.query
const userName = query.userName
return "hello world";
}
JSON 请求体
只有当参数被提前设置时,才能在ctx.body
当中被获取
javascript
export default async function handler(ctx: CloudContext) {
const body = ctx.body
const userName = body.userName
return "hello world";
}
请求头
注意,云函数内部并没有对请求头进行大小写转义,如有需要可自行操作
对于ctx.header
而言无论是否提前被设置,都可以通过如下操作获取到。但是为了程序的高可靠性,还是建议您进行设置。
javascript
export default async function handler(ctx: CloudContext) {
const body = ctx.header // 也可以是ctx.headers
const userAgent = header['User-Agent']
return "hello world";
}