http - 在 gofiber POST 请求中,如何解析请求正文?

标签 http go post go-fiber

如果我将 JSON 数据发布到 gofiber 中的/post route,我将如何读取和更改这些值:

{
    "name" : "John Wick"
    "email" : "johnw@gmail.com"
}
app.Post("/post", func(c *fiber.Ctx) error {
    //read the req.body here
    name := req.body.name
    return c.SendString(name)
}

最佳答案

您可以使用 BodyParser

app.Post("/post", func(c *fiber.Ctx) error {
    payload := struct {
        Name  string `json:"name"`
        Email string `json:"email"`
    }{}

    if err := c.BodyParser(&payload); err != nil {
        return err
    }

    return c.JSON(payload)
}

关于http - 在 gofiber POST 请求中,如何解析请求正文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65334615/

相关文章:

ajax - 当我使用 POSTMAN 在 golang api 上执行 POST 请求时,我成功地收到了 jwt token 作为 cookie,但是当我从浏览器执行此操作时,我没有收到任何 cookie

php - 将特色图片显示为缩略图?

javascript - Angularjs 如何上传多部分表单数据和一个文件?

android - javax.net.ssl.SSLException : hostname in certificate didn't match android 异常

c# - Http无法向服务器发送消息

http - DNS 请求(例如 txt 记录)是否可以用于提供 .css 和 .js 文件?

ios - 将 json 对象从 iphone 发送到 java servlet 的时间太长

go - 在Golang中,json编码和编码有什么区别

php - 如何在 php 中发送 json 请求?

Golang fmt.Println 定时器