【问题标题】:No value gets shown after using gin package to get data from Postman Form使用 gin 包从 Postman 表单获取数据后没有显示任何值
【发布时间】:2020-08-15 03:53:34
【问题描述】:

下面是我正在处理的代码。它在运行时显示成功消息,但不显示 post man 表单中定义的值。相反,它会在命令提示符中显示空格。

package main

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

func saveCustomer(c *gin.Context){
    fn := c.PostForm("firstName")
    ln := c.PostForm("lastName")
    em := c.PostForm("email")
    phnno := c.PostForm("phone_no")
    fmt.Printf("fn: %v; ln: %v ; em: %v ; phnno: %v ;",fn ,ln ,em ,phnno )
    c.String(200, "Success")
}
func main() {
    r := gin.Default()
    r.POST("/saveCustomer", saveCustomer)
    r.Run(":8080")
}

这里不显示任何值,而是显示空格

以下是带有成功消息的邮递员数据字段。

【问题讨论】:

  • 您的代码在我的机器上运行良好,您的邮递员设置一定有问题。另外,这与单元测试或 mongodb 完全无关,所以请编辑这些标签
  • 您的确切代码的屏幕截图(只是将打印更改为不那么混乱)在邮递员和终端日志中运行良好:imgur.com/a/alq2kW1
  • 感谢您查看此内容,删除了标签。在邮递员中也创建了新集合,仍然遇到问题
  • 尝试从头开始创建一个新请求,使用http://localhost:8080 作为 URL,切换到 POST 并设置表单数据字段。如果它仍然不起作用,请尝试使用 curl 请求:curl -X POST \ http://localhost:8080/saveCustomer \ -H 'Cache-Control: no-cache' \ -H 'Postman-Token: 0741a012-7dee-411a-9aa6-695d780025fb' \ -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \ -F firstName=John \ -F lastName=Doe \ -F email=john.doe@test.com \ -F 'phone_no=+12 345 678 901'

标签: go postman go-gin


【解决方案1】:

也许您应该改用 x-www-form-urlencoded?

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", id, page, name, message)
})
router.Run(":8080")

}

数据发布:

POST /post?id=1234&page=1 HTTP/1.1

内容类型:application/x-www-form-urlencoded

name=manu&message=this_is_great

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-20
    • 2019-01-12
    • 1970-01-01
    • 2017-06-23
    • 2022-12-26
    • 2019-08-08
    • 2019-10-12
    • 2021-07-10
    相关资源
    最近更新 更多