使用postman 对go的接口进行请求,需要传入要请求的地址,还有请求类型,如果是POST请求还要有请求的数据等等。
列如
下面展示一些 内联代码片
。
{
"data":"123",
"judge":"ip",
"number":4,
"header_list":[
{
"Content-Length":"http://h.myip.top",
"Host":"GET",
"User-Agent":"123",
"Accept":"*/*",
"Accept-Encoding":"",
"Connection":"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; Tablet PC 2.0; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729)"
}
],
"url":"http://h.myip.top"
}
上面是postman上面请求传的参数
下面是go,main文件的核心 (struct 里面注释的内容是为了在页面如果没有传值,也没有关系,只有特殊的请求这些值才会有变化)
type LoginGetData struct {
Url string `json:"url" form:"url"`
Data string `json:"data" form:"data"`
Judge string `json:"judge" form:"judge"`
Number int `json:"number" form:"number"`
Tps int `json:"tps" form:"tps"`
Host string `json:"Host" form:"Host"`
//ContentLength string `json:"Content-Length"`
//ContentType string `json:"Content-Type"`
//UserAgent string `json:"UserAgent"`
//Accept string `json:"Accept"`
//AcceptEncoding string `json:"AcceptEncoding"`
//Connection string `json:"Connection"`
}
type Resp struct {
Code string `json:"code"`
Msg string `json:"msg"`
}
func Login(c *gin.Context) {
var list LoginGetData
//dataType := c.Request.Header["Content-Type"]
//
//fmt.Println(dataType, 52345435)
var result Resp
err := c.ShouldBind(&list)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
} else {
ContentType := c.DefaultPostForm("Content-Type", "application/json")
Accept := c.DefaultPostForm("Accept", "*/*")
AcceptEncoding := c.DefaultPostForm("Accept-Encoding", "gzip, deflate, br")
UserAgent := c.DefaultPostForm("UserAgent", list.Data)
Connection := c.DefaultPostForm("Connection", "keep-alive")
fmt.Println(list, ContentType, Accept, AcceptEncoding, UserAgent, Connection, 3432432)
if list.Url != "" {
result.Code = "204"
result.Msg = "请求没有传url地址"
}
if list.Host != "" {
req, err := http.NewRequest(list.Host, list.Url, strings.NewReader(list.Data))
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Length", list.Url)
req.Header.Add("Host", list.Host)
req.Header.Add("User-Agent", UserAgent)
req.Header.Add("Accept", Accept)
req.Header.Add("Accept-Encoding", AcceptEncoding)
req.Header.Add("Connection", Connection)
req.Header.Add("Content-Type", ContentType)
client := &http.Client{}
var start_time, cost_time int
start_time = int(time.Now().UnixNano() / 1e6)
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
lin, _ := ioutil.ReadAll(resp.Body)
end_time := int(time.Now().UnixNano() / 1e6)
cost_time = end_time - start_time
if resp != nil {
defer resp.Body.Close()
}
if resp.StatusCode != 200 {
// fmt.Println("StatusCode", resp.Header, string(lin))
fmt.Printf("Expect 200, %d GET!", resp.StatusCode)
//return false, 0
}
if strings.Index(string(lin), list.Judge) != -1 {
c.JSON(http.StatusOK, gin.H{
"code": "200",
"msg": "请求成功",
"time": time.Now().Unix(),
"data": string(lin),
"cost_time": cost_time,
})
} else {
result.Code = "204"
result.Msg = "请求类型错误"
}
}
}
}
请求如果不成功,要检查route这个文件
package route
import (
"github.com/gin-gonic/gin"
"hello/api/v1"
)
func Http(router *gin.Engine) {
apiRouter := router.Group("/api/v1/")
{
apiRouter.POST("/login", v1.Login)
}
}
我的接口是api/v1/login 路由不要错了,其他应该是没有问题了,有问题可以私聊我哦。