1 Star 0 Fork 0

Wsage/go-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ctx.go 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
王少奇 提交于 2021-09-09 16:47 . 恢复
package go_framework
import (
"encoding/json"
"io/ioutil"
"net/http"
)
//请求上下文数据结构
type Ctx struct {
Response http.ResponseWriter
Request *http.Request
Data map[string]interface{}
}
//获取请求参数(多个)
func (ctx Ctx) HttpInput(keys []string) (map[string]string, error) {
r := ctx.Request
if err := r.ParseForm(); err != nil {
return nil, err
}
m := make(map[string]string)
for _, key := range keys {
m[key] = r.FormValue(key)
}
return m, nil
}
//获取请求参数(单个)
func (ctx Ctx) HttpInputOne(key string) (string, error) {
m,err:=ctx.HttpInput([]string{key})
if err != nil {
return "", err
}
return m[key], nil
}
//从body获取input
func (ctx Ctx) HttpInputFromBody(keys []string) (map[string]string, error) {
r := ctx.Request
con, _ := ioutil.ReadAll(r.Body) //获取post的数据
m := make(map[string]string)
json.Unmarshal(con,&m);
return m, nil
}
//跳转
func (ctx *Ctx) Redirect(url string) {
http.Redirect(ctx.Response, ctx.Request, url, http.StatusFound)
}
//以json响应
func (ctx *Ctx) ResponseJson(out interface{}) {
b, _ := json.Marshal(out)
ctx.Response.Write(b)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/scottq/go-framework.git
git@gitee.com:scottq/go-framework.git
scottq
go-framework
go-framework
v1.1.40

搜索帮助