从POST请求中获取文本的方法是使用Go Gin-Gonic框架。

huangapple go评论112阅读模式
英文:

Go Gin-Gonic, get text from POST request

问题

我正在开发一个使用Go和Gin-Gonic包的REST API。我的想法是创建一个REST API,它接收JSON格式的POST请求,并将此调用重定向到另一个应用程序(也是一个API)。以下是一段代码:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. )
  7. func main() {
  8. r := gin.Default()
  9. r.GET("/status", func(c *gin.Context) {
  10. c.String(200, "on")
  11. })
  12. r.GET("/user/:name", func(c *gin.Context) {
  13. name := c.Param("name")
  14. c.String(http.StatusOK, "Hello %s", name)
  15. })
  16. r.GET("/user/:name/:action", func(c *gin.Context) {
  17. name := c.Param("name")
  18. action := c.Param("action")
  19. message := name + " is " + action
  20. c.String(http.StatusOK, message)
  21. })
  22. r.POST("/foo", func(c *gin.Context) {
  23. fmt.Printf("%s", "At least I got here")
  24. message := c.PostForm() //???
  25. c.JSON(200, gin.H{"status": message}) //???
  26. })
  27. r.Run(":8080") // listen and serve on 0.0.0.0:8080
  28. }

r.POST("/foo",...)函数中,我希望c.JSON将我发送的完整JSON返回给我:

  1. curl -H "Content-Type: application/json" -X POST -d '{"user":"xyz","password":"xyz"}' http://localhost:8080/foo

我看过一些示例,它们通过创建一个与输入JSON具有相同结构的结构体来绑定JSON文件(请参阅Gin-Gonic的示例https://github.com/gin-gonic/gin)。然而,我只需要重新发送完整的字符串,而不必关心格式。有什么想法吗?

英文:

I'm starting to develop a REST API using Go and package Gin-Gonic. The idea is to create a REST API that receives POST requests in a JSON format and redirects this call to another application (also a API). Here is a piece of code:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. )
  7. func main() {
  8. r := gin.Default()
  9. r.GET("/status", func(c *gin.Context) {
  10. c.String(200, "on")
  11. })
  12. r.GET("/user/:name", func(c *gin.Context) {
  13. name := c.Param("name")
  14. c.String(http.StatusOK, "Hello %s", name)
  15. })
  16. r.GET("/user/:name/:action", func(c *gin.Context) {
  17. name := c.Param("name")
  18. action := c.Param("action")
  19. message := name + " is " + action
  20. c.String(http.StatusOK, message)
  21. })
  22. r.POST("/foo", func(c *gin.Context) {
  23. fmt.Printf("%s", "At least I got here")
  24. message := c.PostForm() //???
  25. c.JSON(200, gin.H{"status": message}) //???
  26. })
  27. r.Run(":8080") // listen an
  28. }

At function r.Posts("/foo",...) I would like c.JSON to send me back the full JSON that I sent:

  1. curl -H "Content-Type: application/json" -X POST -d '{"user":"xyz","password":"xyz"}' http://localhost:8080/foo

I've seen examples where they bind the JSON file by creating a struct with the same structure as the input JSON (check Gin-Gonic examples at https://github.com/gin-gonic/gin ). However I only need to resend the full string without taking care of the format. Any ideas?

答案1

得分: 19

请尝试以下示例:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. )
  7. type LOGIN struct{
  8. USER string `json:"user" binding:"required"`
  9. PASSWORD string `json:"password" binding:"required"`
  10. }
  11. func main() {
  12. r := gin.Default()
  13. r.GET("/status", func(c *gin.Context) {
  14. c.String(200, "on")
  15. })
  16. r.GET("/user/:name", func(c *gin.Context) {
  17. name := c.Param("name")
  18. c.String(http.StatusOK, "Hello %s", name)
  19. })
  20. r.GET("/user/:name/:action", func(c *gin.Context) {
  21. name := c.Param("name")
  22. action := c.Param("action")
  23. message := name + " is " + action
  24. c.String(http.StatusOK, message)
  25. })
  26. r.POST("/foo", func(c *gin.Context) {
  27. var login LOGIN
  28. c.BindJSON(&login)
  29. c.JSON(200, gin.H{"status": login.USER}) // 在这里自定义你的响应
  30. })
  31. r.Run(":8080") // 监听传入的连接
  32. }

希望对你有帮助!

英文:

Try this example:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. )
  7. type LOGIN struct{
  8. USER string `json:"user" binding:"required"`
  9. PASSWORD string `json:"password" binding:"required"`
  10. }
  11. func main() {
  12. r := gin.Default()
  13. r.GET("/status", func(c *gin.Context) {
  14. c.String(200, "on")
  15. })
  16. r.GET("/user/:name", func(c *gin.Context) {
  17. name := c.Param("name")
  18. c.String(http.StatusOK, "Hello %s", name)
  19. })
  20. r.GET("/user/:name/:action", func(c *gin.Context) {
  21. name := c.Param("name")
  22. action := c.Param("action")
  23. message := name + " is " + action
  24. c.String(http.StatusOK, message)
  25. })
  26. r.POST("/foo", func(c *gin.Context) {
  27. var login LOGIN
  28. c.BindJSON(&login)
  29. c.JSON(200, gin.H{"status": login.USER}) // Your custom response here
  30. })
  31. r.Run(":8080") // listen for incoming connections
  32. }

答案2

得分: -1

我最终创建了一个结构体来解析我的JSON,然后进行了一些必要的计算,最后我使用json.Marshal将解析后的数据重新发送为字符串形式。我认为解析JSON是有意义的,这是一种检查接收到的信息是否正确的方式。

英文:

I ended up creating a struct to parse my JSON, then I make some required calculations and finally I resend the data parsing my JSON to string using json.Marshal. I think it makes sense to parse the JSON, it is a way to check whether the info received is correct.

huangapple
  • 本文由 发表于 2017年2月15日 19:24:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/42247978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定