返回第三方数据
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
//返回第三方获取的数据
func main() {
router := gin.Default()
router.GET("/someDataFromReader", func(c *gin.Context) {
response, err := http.Get("https://rolingg.top/images/head.jpg") //获取链接的指定数据
if err != nil || response.StatusCode != http.StatusOK { //错误反馈
c.Status(http.StatusServiceUnavailable) //如果有错反馈503码
return
}
reader := response.Body //获取数据的本体
contentLength := response.ContentLength //获取数据的长度
contentType := response.Header.Get("Content-Type") //获取数据的响应头内容
extraHeaders := map[string]string{
"Content-Disposition": `attachment; filename="header.png"`, //自定义而外响应头数据
}
c.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders) //将前面的读取写入流中并更新http码(官方原句:DataFromReader writes the specified reader into the body stream and updates the HTTP code)
})
router.Run(":80")
}
评论(0)