我想使用 curl 测试我的 golang rest api。
我曾经这样做的命令:
curl -X POST -H "Content-Type: application/json" -d '{"username":"username","password":"password"}' "http://localhost:8000/api/rooms/signin"
我写的简化版服务器
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
fmt.Println("Listening on port 8000")
router := mux.NewRouter()
router.HandleFunc("/api/rooms/signin", Signin)
log.Fatal(http.ListenAndServe(":8000", router))
}
func Signin(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
fmt.Println("POST")
if err := r.ParseForm(); err != nil {
fmt.Println("parsing failed")
return
}
fmt.Println("parsing success")
fmt.Println(r.Form)
fmt.Println("path: ", r.URL.Path)
fmt.Println("scheme: ", r.URL.Scheme)
username := r.FormValue("username")
password := r.FormValue("password")
fmt.Println(username)
fmt.Println(password)
fmt.Println("username:", r.Form["username"])
fmt.Println("password:", r.Form["password"])
return
}
}
问题是我得到一个空表格——这是我得到的输出
Listening on port 8000
POST
parsing success
map[]
path: /api/rooms/signin
scheme:
username: []
password: []
我想这是不对的,但我想不出我可能做错了什么。预期的输出是将“密码”和“用户名”字符串作为 golang 服务器的输出。
莫回无
相关分类