gin中从reader读取数据数据

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"reflect"
)

func main() {
	// 从 reader 读取数据
	router := gin.Default()
	router.GET("/someDateFromReader", func(context *gin.Context) {
		response, err := http.Get("https://raw.githubusercontent.com/gin-gonic/logo/master/color.png")
		if err != nil || response.StatusCode != http.StatusOK {
			context.Status(http.StatusServiceUnavailable)
			return
		}
		reader := response.Body
		fmt.Println(reader, reflect.TypeOf(reader))  // {0xc0004266e0} http.http2transportResponseBody
		contentLength := response.ContentLength
		contentType := response.Header.Get("Content-Type")
		extraHeaders := map[string]string{
			"Content-Disposition": `attachment; filename="gopher.png"`,
			"cookie": `aaazzzxxxbbb"`,
		}
		context.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)

	})
	router.Run()
}

  

posted @ 2021-10-21 15:55  专职  阅读(160)  评论(0编辑  收藏  举报