稍微大型的项目可能都会有个中转项目,今天就说碰到的文件上传中转的时候的问题,如果直接使用POST请求作为中转就会爆出这个错误,
直接上代码
type YGHTTPClient struct {
*resty.Client
context context.Context
timeout time.Duration
}
func NewHTTP(ctx context.Context) *YGHTTPClient {
client := new(YGHTTPClient)
client.Client = resty.New()
client.context = ctx
return client
}
func Upload(c *gin.Context){
headers := make(map[string]string)
headers["Content-Type"] = c.Request.Header.Get("Content-Type") // content-type: application/json; charset=utf-8
fileHeader, _ := c.FormFile("file")
file, _ := fileHeader.Open()
resp, err := NewHTTP(ctx).Client.SetHeaders(headers).R().SetFileReader("file", fileHeader.Filename, file).Post(url)
}