From 249e749e6cd8f6f379aca30c52a1c20ca45bf3ed Mon Sep 17 00:00:00 2001 From: jjxu <428192774@qq.com> Date: Thu, 16 Nov 2023 14:55:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96bytes=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- excample/simpleRequest_test.go | 10 +++++++--- simpleRequest.go | 4 ++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/excample/simpleRequest_test.go b/excample/simpleRequest_test.go index 12e2004..c2c848f 100644 --- a/excample/simpleRequest_test.go +++ b/excample/simpleRequest_test.go @@ -11,6 +11,7 @@ import ( "fmt" "github.com/dorlolo/simpleRequest" "net/http" + "os" "strings" "testing" "time" @@ -133,6 +134,7 @@ func TestTextPlain(t *testing.T) { } +// 阿里云Oss文件上传示例 func TestUploadFileToOss(t *testing.T) { var signedUrl = "" //STS授权url var xOssCallback = "" //回调信息 @@ -140,9 +142,11 @@ func TestUploadFileToOss(t *testing.T) { req.Headers(). Sets(map[string]string{ "X-Oss-Callback": xOssCallback, - }). - Omit("Content-Type") //oss默认不支持传Content-Type,需要忽略掉。或者可以在oss控制台上放行这个请求头 - req.Body().SetFromDataFile("file", "./CHANGELOG.MD") + }) + + fileData, err := os.ReadFile("./CHANGELOG.MD") + req.Body().SetBytes(fileData) + body, err := req.PUT(signedUrl) if err != nil { t.Error(err) diff --git a/simpleRequest.go b/simpleRequest.go index 148f35b..803998c 100644 --- a/simpleRequest.go +++ b/simpleRequest.go @@ -8,6 +8,7 @@ package simpleRequest import ( + "bytes" "crypto/tls" "fmt" "io" @@ -286,6 +287,9 @@ func (s *SimpleRequest) initBody() { } s.body = parser.Unmarshal(s.BodyEntryMark, s.BodyEntries) + case contentTypeData == "" && s.BodyEntryMark == BytesEntryType: + s.body = bytes.NewReader(s.BodyEntries[BytesEntryType.string()].([]byte)) + case contentTypeData == "" || strings.Contains(contentTypeData, "form-urlencoded"): //default header type is "x-www-form-urlencoded" var parser, ok = s.bodyEntryParsers["form-urlencoded"]