gin BindJSON

r:=gin.New()
r.GET("/", func(c *gin.Context) {
var cc SSHconfig
if err:=c.BindJSON(&cc);err!=nil{
log.Println(err)
}
var (
//u model.CoreAccount
p model.CoreGrained
//groupList []model.CoreRoleGroup
//s model.CoreGlobalConfiguration
)
model.DB().Debug().Select("`group`").Where("username =?", cc.Username).First(&p)
c.AbortWithStatusJSON(200,gin.H{"OK":true,"data":p.Group})

})

r.Run("127.0.0.1:8000")


}

 

 只能在raw访问,有点想不明白?

 

我们后端用的是go的gin server,一共部署3个实例。

package main

import (
    "github.com/gin-gonic/gin"
	"flag"
    "strconv"
	"fmt"
)


func GetResult(c *gin.Context) {

	a,_ := strconv.Atoi(c.Query("a"))
	b,_ := strconv.Atoi(c.Query("b"))

	uid := c.Request.Header.Get("uid")
	requestid := c.Request.Header.Get("requestid")

	fmt.Printf("uid=%s\n", uid)
	fmt.Printf("requestid=%s\n", requestid)

	result := a+b

	c.JSON(200, gin.H{
		"result": result,
	})
}


func main() {
	var port string
	flag.StringVar(&port, "p", "7777", "port")
	flag.Parse()

    r := gin.Default()

    // 普通用户接口
    r.GET("/get_result_http", GetResult)

    r.Run(":" + port) 

}

启动

./gin_server -p 7776 &
./gin_server -p 7777 &
./gin_server -p 7778 &

在APISIX的Dashboard 配置以下路由

 

配置插件

把我们的my-rewrite-response配置进去,这里tag先不配值。

{
  "conf": [
    {
      "name": "my-rewrite-response",
      "value": "{\"tag\":\"\"}"
    }
  ]
}

提交后尝试请求APISIX,看到返回的header带上了responseid。

root@JamesLee:/mnt/d/code/apisix_test/gin_server# curl -i "http://127.0.0.1:9080/get_result_http?a=6&b=3" -H 'username: lijunshi' -H 'password: lijunshipwd'
HTTP/1.1 200 OK
Date: Sun, 12 Mar 2023 12:56:49 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
responseid: 409384d5-0985-486b-be34-5627628a266e
Server: APISIX/3.1.0

{"result":9}

my-rewrite-response配置为

{
  "conf": [
    {
      "name": "my-rewrite-response",
      "value": "{\"tag\":\"hello my-rewrite-response\"}"
    }
  ]
}

再次请求,这时header和body都被改写了。

root@JamesLee:/mnt/d/code/apisix_test/gin_server# curl -i "http://127.0.0.1:9080/get_result_http?a=6&b=3" -H 'username: lijunshi' -H 'password: lijunshipwd'
HTTP/1.1 200 OK
Date: Sun, 12 Mar 2023 12:58:06 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
responseid: 5641684d-d4eb-4789-98ea-fbf1c96fd919
Server: APISIX/3.1.0

hello my-rewrite-response
posted @ 2022-04-07 15:51  技术颜良  阅读(764)  评论(0编辑  收藏  举报