前端小蜜蜂
首页/列表页/详情页/
go 中转文件上传 multipart/form-data;
2022-04-251052

稍微大型的项目可能都会有个中转项目,今天就说碰到的文件上传中转的时候的问题,如果直接使用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)
}
王者荣耀英雄联盟穿越火线
想写一首诗送给你
却发现早已没有资格
想写一句话送给我自己
不知何时
心如死灰