重定向
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
// 外部链接
r.GET("/redirect1", func(context *gin.Context) {
url := "http://www.baidu.com"
context.Redirect(http.StatusMovedPermanently, url)
})
// 自定义重定向
r.GET("/redirect2", func(context *gin.Context) {
context.Request.URL.Path = "/testRedirect"
r.HandleContext(context)
})
// 自定义链接
r.GET("/testRedirect", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"msg": "响应成功",
})
})
r.Run(":9090")
}
获取第三方数据
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.GET("/getOtherData", func(context *gin.Context) {
url := "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png"
// 使用http.Get方法向指定的URL发送一个GET请求,并接收响应
res, err := http.Get(url)
// 检查是否在请求过程中发生错误,或者响应的状态码是否不是200(OK)
if err != nil || res.StatusCode != http.StatusOK {
context.Status(http.StatusServiceUnavailable)
return
}
// 请求体内容
body := res.Body
// 请求体长度
contentLen := res.ContentLength
// 请求媒体类型
contentType := res.Header.Get("Content-Type")
// 将获取到的数据以及相关信息发送给客户端
context.DataFromReader(http.StatusOK, contentLen, contentType, body, nil)
})
r.Run(":9090")
}
多形式渲染
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.GET("/json", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{
"html": "<b>hello</b>",
})
})
r.GET("/html", func(context *gin.Context) {
context.PureJSON(http.StatusOK, gin.H{
"html": "<b>hello</b>",
})
})
r.GET("/xml", func(context *gin.Context) {
type Message struct {
Name string
Msg string
Age int
}
info := Message{}
info.Name = "FAN"
info.Msg = "hello"
info.Age = 23
context.XML(http.StatusOK, info)
})
r.GET("yaml", func(context *gin.Context) {
context.YAML(http.StatusOK, gin.H{
"message": "hello",
"status": 200,
})
})
r.Run(":9090")
}
文件服务器
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/file", fileServer)
r.Run(":9090")
}
func fileServer(context *gin.Context) {
// 存储路径
path := "D:/2-golang/img/"
// 文件名
fileName := path + context.Query("name")
context.File(fileName)
}
单文件上传
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.POST("/upload", func(context *gin.Context) {
file, err := context.FormFile("fileName")
if err != nil {
context.String(http.StatusBadRequest, "文件上传错误!")
}
dst := "D:/2-golang/img/"
context.SaveUploadedFile(file, dst+file.Filename)
context.String(http.StatusOK, fmt.Sprintf("%s 上传成功", file.Filename))
})
r.Run(":9090")
}
多文件上传
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.POST("/allUpload", fileServe)
r.Run(":9090")
}
func fileServe(context *gin.Context) {
form, err := context.MultipartForm()
if err != nil {
context.String(http.StatusBadRequest, "上传文件错误!")
}
// 多文件上传原理是一样的,只是多了个循环来保存文件
dst := "D:/2-golang/img/"
files := form.File["file_key"]
for _, file := range files {
context.SaveUploadedFile(file, dst+file.Filename)
}
context.String(http.StatusOK, fmt.Sprintf("%d 个文件上传成功!", len(files)))
}