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

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

Printf not working in http handler in golang

问题

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

在handler/users中

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

在main.go中

router.Handle("/", MustLogin(http.HandlerFunc(handlers.GetHome))).Methods("GET")

router.HandleFunc("/signup", handlers.GetSignup).Methods("GET")
router.HandleFunc("/signup", handlers.PostSignup).Methods("POST")
router.HandleFunc("/login", handlers.GetLogin).Methods("GET")
router.HandleFunc("/login", handlers.PostLogin).Methods("POST")
router.HandleFunc("/app/signup", handlers.AppPostSignup).Methods("POST")
router.HandleFunc("/authenticate", handlers.Authenticate).Methods("POST")
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

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

In main.go

router.Handle("/", MustLogin(http.HandlerFunc(handlers.GetHome))).Methods("GET")

router.HandleFunc("/signup", handlers.GetSignup).Methods("GET")
router.HandleFunc("/signup", handlers.PostSignup).Methods("POST")
router.HandleFunc("/login", handlers.GetLogin).Methods("GET")
router.HandleFunc("/login", handlers.PostLogin).Methods("POST")
router.HandleFunc("/app/signup", handlers.AppPostSignup).Methods("POST")
router.HandleFunc("/authenticate", handlers.Authenticate).Methods("POST")
router.HandleFunc("/logout", handlers.GetLogout).Methods("GET")

What did I do wrong?

答案1

得分: 4

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

在AppPostSignup中

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

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

curl -X POST localhost:8888/app/signup

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

英文:

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

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:

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:

确定