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"]