英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论