mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
ghttp post及文件上传调试
This commit is contained in:
@ -8,10 +8,14 @@
|
||||
package ghttp
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"os"
|
||||
"io"
|
||||
"time"
|
||||
"bytes"
|
||||
"strings"
|
||||
"net/http"
|
||||
"mime/multipart"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// http客户端
|
||||
@ -40,8 +44,45 @@ func (c *Client) Put(url, data string) (*ClientResponse, error) {
|
||||
}
|
||||
|
||||
// POST请求提交数据
|
||||
// 支持文件上传,需要字段格式为:FieldName=@file:
|
||||
func (c *Client) Post(url, data string) (*ClientResponse, error) {
|
||||
resp, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data))
|
||||
isfile := false
|
||||
buffer := new(bytes.Buffer)
|
||||
writer := multipart.NewWriter(buffer)
|
||||
for _, item := range strings.Split(data, "&") {
|
||||
array := strings.Split(item, "=")
|
||||
// 判断是否文件上传
|
||||
if len(array[1]) > 6 && strings.Compare(array[1][0:6], "@file:") == 0 {
|
||||
isfile = true
|
||||
if file, err := writer.CreateFormFile(array[0], array[1][6:]); err == nil {
|
||||
if f, err := os.Open(array[1][6:]); err == nil {
|
||||
defer f.Close()
|
||||
if _, err = io.Copy(file, f); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
writer.WriteField(array[0], array[1])
|
||||
}
|
||||
}
|
||||
writer.Close()
|
||||
req, err := http.NewRequest("POST", url, buffer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 表单类型处理
|
||||
if isfile {
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
} else {
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
// 执行请求
|
||||
resp, err := c.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -77,10 +118,15 @@ func (c *Client) Trace(url, data string) (*ClientResponse, error) {
|
||||
|
||||
// 请求并返回response对象,该方法支持二进制提交数据
|
||||
func (c *Client) DoRequest(method, url string, data []byte) (*ClientResponse, error) {
|
||||
//if strings.Compare("POST", strings.ToUpper(method)) == 0 {
|
||||
// return c.Post(url, string(data))
|
||||
//}
|
||||
fmt.Println(method)
|
||||
req, err := http.NewRequest(strings.ToUpper(method), url, bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := c.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -100,7 +146,7 @@ func Put(url, data string) (*ClientResponse, error) {
|
||||
}
|
||||
|
||||
func Post(url, data string) (*ClientResponse, error) {
|
||||
return DoRequest("PUT", url, []byte(data))
|
||||
return DoRequest("POST", url, []byte(data))
|
||||
}
|
||||
|
||||
func Delete(url, data string) (*ClientResponse, error) {
|
||||
|
||||
@ -12,6 +12,7 @@ import (
|
||||
"net/url"
|
||||
"gitee.com/johng/gf/g/util/gconv"
|
||||
"gitee.com/johng/gf/g/encoding/gjson"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// 请求对象
|
||||
@ -86,6 +87,10 @@ func (r *Request) GetQueryMap(defaultMap map[string]string) map[string]string {
|
||||
|
||||
// 获得post参数
|
||||
func (r *Request) GetPost(k string) []string {
|
||||
if len(r.PostForm) == 0 {
|
||||
r.ParseForm()
|
||||
fmt.Println(r)
|
||||
}
|
||||
if v, ok := r.PostForm[k]; ok {
|
||||
return v
|
||||
}
|
||||
|
||||
46
geg/frame/mvc/controller/demo/upload.go
Normal file
46
geg/frame/mvc/controller/demo/upload.go
Normal file
@ -0,0 +1,46 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
"gitee.com/johng/gf/g/os/glog"
|
||||
"gitee.com/johng/gf/g/os/gfile"
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func Upload(r *ghttp.Request) {
|
||||
fmt.Println(r.GetPostMap(nil))
|
||||
fmt.Println(r.GetPostString("name"))
|
||||
//fmt.Println(string(r.GetRaw()))
|
||||
return
|
||||
if f, h, e := r.FormFile("upload-file"); e == nil {
|
||||
defer f.Close()
|
||||
buffer := make([]byte, h.Size)
|
||||
f.Read(buffer)
|
||||
gfile.PutBinContents("/tmp/" + h.Filename, buffer)
|
||||
r.Response.WriteString(fmt.Sprintf("%s upload success, input value:%s", h.Filename, r.GetPostString("name")))
|
||||
} else {
|
||||
glog.Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
func UploadShow(r *ghttp.Request) {
|
||||
r.Response.WriteString(`
|
||||
<html>
|
||||
<head>
|
||||
<title>上传文件</title>
|
||||
</head>
|
||||
<body>
|
||||
<form enctype="multipart/form-data" action="/upload" method="post">
|
||||
<input type="input" name="name" />
|
||||
<input type="file" name="upload-file" />
|
||||
<input type="submit" value="upload" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
}
|
||||
|
||||
func init() {
|
||||
ghttp.GetServer().BindHandler("/upload", Upload)
|
||||
ghttp.GetServer().BindHandler("/upload/show", UploadShow)
|
||||
}
|
||||
@ -3,7 +3,6 @@ package demo
|
||||
import (
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
"gitee.com/johng/gf/g/frame/gmvc"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// 定义业务相关的控制器对象,
|
||||
|
||||
11
geg/frame/mvc/view/upload/upload.tpl
Normal file
11
geg/frame/mvc/view/upload/upload.tpl
Normal file
@ -0,0 +1,11 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>上传文件</title>
|
||||
</head>
|
||||
<body>
|
||||
<form enctype="multipart/form-data" action="/upload" method="post">
|
||||
<input type="file" name="upload-file" />
|
||||
<input type="submit" value="upload" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
11
geg/net/ghttp/upload_client.go
Normal file
11
geg/net/ghttp/upload_client.go
Normal file
@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitee.com/johng/gf/g/net/ghttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
_, e := ghttp.Post("http://127.0.0.1:8199/upload", "name=john&age=18")
|
||||
fmt.Println(e)
|
||||
}
|
||||
Reference in New Issue
Block a user