Ai
1 Star 0 Fork 0

kade/librarys

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
post_form.go 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
kade 提交于 2024-03-26 16:58 +08:00 . v1
package form
import (
"encoding/json"
"github.com/emicklei/go-restful/v3"
)
const (
MIME_POST_FORM = "application/x-www-form-urlencoded"
)
// NewEntityAccessorJSON returns a new EntityReaderWriter for accessing Form content.
// This package is already initialized with such an accessor using the MIME_POST_FORM contentType.
func NewEntityAccessorPostForm() restful.EntityReaderWriter {
return entityPostFormAccess{ContentType: MIME_POST_FORM}
}
type entityPostFormAccess struct {
// This is used for setting the Content-Type header when writing
ContentType string
}
// Read unmarshalls the value from Form
func (e entityPostFormAccess) Read(req *restful.Request, v interface{}) error {
if err := req.Request.ParseForm(); err != nil {
return err
}
return mapForm(v, req.Request.PostForm)
}
// Write marshalls the value to Form and set the Content-Type Header.
func (e entityPostFormAccess) Write(resp *restful.Response, status int, v interface{}) error {
return writeJSON(resp, status, restful.MIME_JSON, v)
}
// write marshalls the value to YAML and set the Content-Type Header.
func writeJSON(resp *restful.Response, status int, contentType string, v interface{}) error {
if v == nil {
resp.WriteHeader(status)
// do not write a nil representation
return nil
}
resp.Header().Set(restful.HEADER_ContentType, contentType)
resp.WriteHeader(status)
return json.NewEncoder(resp).Encode(v)
}
func init() {
restful.RegisterEntityAccessor(MIME_POST_FORM, NewEntityAccessorPostForm())
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/go-kade/librarys.git
git@gitee.com:go-kade/librarys.git
go-kade
librarys
librarys
70cb23a27fa4

搜索帮助