-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
45 lines (39 loc) · 1.11 KB
/
export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package gin
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"net/url"
"github.com/gin-gonic/gin"
)
type ExportService struct{}
func (*ExportService) request(ctx *gin.Context, api string, response interface{}) error {
/* req,_:=http.NewRequest(ctx.Method(),api,ctx.Request().Body)
req.Header["HeaderAuthorization"] = []string{ctx.GetHeader("HeaderAuthorization")}
req.Header["Content-Type"] = []string{"application/json"}*/
req := ctx.Request //原请求头可能会干扰返回
req.URL, _ = url.Parse(api)
req.RequestURI = ""
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
ctx.Writer.WriteString(err.Error())
return err
}
defer resp.Body.Close()
var buf bytes.Buffer
buf.ReadFrom(resp.Body)
if err := json.Unmarshal(buf.Bytes(), &response); err != nil {
log.Println(err)
ctx.Writer.Write(buf.Bytes())
return err
}
return nil
}
func (*ExportService) response(ctx *gin.Context, filename string, f io.WriterTo) {
ctx.Header("Content-Type", "application/octet-stream")
ctx.Header("Content-Disposition", "attachment;filename="+filename)
f.WriteTo(ctx.Writer)
}