在Golang的HTTP处理程序中,Printf不起作用。

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

Printf not working in http handler in golang

问题

我正在尝试使用Go-bootstrap库编写一个Web服务器,并且我已经在handler/users中编写了自己的处理程序,该处理程序在localhost/app/signup上的POST请求时被调用。我试图将JSON数据打印到终端,但是fmt.Printf()没有打印任何内容。文件如下所示:

在handler/users中

  1. func AppPostSignup(w http.ResponseWriter, r *http.Request) {
  2. w.Header().Set("Content-Type", "application/json")
  3. fmt.Println("In AppPostSignup")
  4. data := map[string]interface{}{}
  5. body, _ := ioutil.ReadAll(r.Body)
  6. json.Unmarshal(body, &data)
  7. db := context.Get(r, "db").(*sqlx.DB)
  8. email := data["Email"]
  9. password := data["Password"]
  10. passwordAgain := data["PasswordAgain"]
  11. fmt.Printf("\ntype : %T\nData: %v", email, email)
  12. _, err := dal.NewUser(db).Signup(nil, email.(string), password.(string), passwordAgain.(string))
  13. if err != nil {
  14. libhttp.HandleErrorJson(w, err)
  15. return
  16. }
  17. }

在main.go中

  1. router.Handle("/", MustLogin(http.HandlerFunc(handlers.GetHome))).Methods("GET")
  2. router.HandleFunc("/signup", handlers.GetSignup).Methods("GET")
  3. router.HandleFunc("/signup", handlers.PostSignup).Methods("POST")
  4. router.HandleFunc("/login", handlers.GetLogin).Methods("GET")
  5. router.HandleFunc("/login", handlers.PostLogin).Methods("POST")
  6. router.HandleFunc("/app/signup", handlers.AppPostSignup).Methods("POST")
  7. router.HandleFunc("/authenticate", handlers.Authenticate).Methods("POST")
  8. router.HandleFunc("/logout", handlers.GetLogout).Methods("GET")

我做错了什么?

英文:

I am trying to write a web server using Go-bootstrap library and I have written my own handler in handler/users which is called on a post request on
localhost/app/signup. I am trying to print the json data to the terminal but fmt.Printf() prints nothing. The files are as follows:

In handler/users

  1. func AppPostSignup(w http.ResponseWriter, r *http.Request) {
  2. w.Header().Set("Content-Type", "application/json")
  3. fmt.Println("In AppPostSignup")
  4. data := map[string]interface{}{}
  5. body, _ := ioutil.ReadAll(r.Body)
  6. json.Unmarshal(body, &data)
  7. db := context.Get(r, "db").(*sqlx.DB)
  8. email := data["Email"]
  9. password := data["Password"]
  10. passwordAgain := data["PasswordAgain"]
  11. fmt.Printf("\ntype : %T\nData: %v", email, email)
  12. _, err := dal.NewUser(db).Signup(nil, email.(string), password.(string), passwordAgain.(string))
  13. if err != nil {
  14. libhttp.HandleErrorJson(w, err)
  15. return
  16. }
  17. }

In main.go

  1. router.Handle("/", MustLogin(http.HandlerFunc(handlers.GetHome))).Methods("GET")
  2. router.HandleFunc("/signup", handlers.GetSignup).Methods("GET")
  3. router.HandleFunc("/signup", handlers.PostSignup).Methods("POST")
  4. router.HandleFunc("/login", handlers.GetLogin).Methods("GET")
  5. router.HandleFunc("/login", handlers.PostLogin).Methods("POST")
  6. router.HandleFunc("/app/signup", handlers.AppPostSignup).Methods("POST")
  7. router.HandleFunc("/authenticate", handlers.Authenticate).Methods("POST")
  8. router.HandleFunc("/logout", handlers.GetLogout).Methods("GET")

What did I do wrong?

答案1

得分: 4

我刚刚测试了你的代码,并在控制台上看到了以下输出:

  1. AppPostSignup

我猜你试图使用GET请求测试你的/app/signup端点,但你将其标记为POST请求。

如果你确实希望它是一个POST请求,那么你可以使用curl进行测试,像这样:

  1. curl -X POST localhost:8888/app/signup

注意,该请求缺少有效的请求体,但你将能够看到你的消息被打印出来。

英文:

I just tested your code and I saw the following printed to the console:

  1. In AppPostSignup

My guess is that you tried to test your /app/signup endpoint using a GET request, when you have it marked only as a POST request.

If you really want it to be a POST request, then you can test it with curl like so:

  1. curl -X POST localhost:8888/app/signup

Note, that request is missing a valid body, but you will be able to see your message printed.

huangapple
  • 本文由 发表于 2015年12月30日 13:45:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/34523627.html
匿名

发表评论

匿名网友

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

确定