【问题标题】:Go gin-framework: Testing query and POST with cURLGo gin-framework:使用 cURL 测试查询和 POST
【发布时间】:2016-08-17 05:37:54
【问题描述】:

我正在尝试the README of gin framework 中的代码示例(“另一个示例:查询 + 发布表单”):

package main

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

func main() {
    router := gin.Default()

    router.POST("/post", func(c *gin.Context) {
        id := c.Query("id")
        page := c.DefaultQuery("page", "0")
        name := c.PostForm("name")
        message := c.PostForm("message")

        fmt.Printf("id: %s; page: %s; name: %s; message: %s\n", id, page, name, message)
    })
    router.Run(":8080")
}

使用 cURL 测试代码:

curl -d "name=Maru&message=Nice" 0.0.0.0:8080/post?id=2&page=3

服务器返回:id: 2; page: 0; name: Maru; message: Nice

卷曲测试是否正确?为什么返回值中的page不等于3?

【问题讨论】:

    标签: http curl go go-gin


    【解决方案1】:

    & 符号 (&) 是 shell 中的一个特殊字符。它会导致上一个命令在后台运行。您的 shell 将命令解释为:

    curl -d "name=Maru&message=Nice" 0.0.0.0:8080/post?id=2 & # run curl in the background
    page=3 # set page=3
    

    转义字符会给你预期的结果:

    curl -d "name=Maru&message=Nice" "0.0.0.0:8080/post?id=2&page=3"
    
    curl -d "name=Maru&message=Nice" 0.0.0.0:8080/post?id=2\&page=3
    

    【讨论】:

      猜你喜欢
      • 2021-10-08
      • 2022-01-03
      • 2022-06-30
      • 2021-02-26
      • 2021-06-17
      • 1970-01-01
      • 2020-08-23
      • 2017-07-04
      • 2017-12-30
      相关资源
      最近更新 更多