I would like to test my golang rest api using curl.
Command I used to do it:
curl -X POST -H "Content-Type: application/json" -d '{"username":"username","password":"password"}' "http://localhost:8000/api/rooms/signin"
simplified version of server I have written
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
}
}
The problem is I get an empty form - this is the output I get
Listening on port 8000
POST
parsing success
map[]
path: /api/rooms/signin
scheme:
username: []
password: []
I suppose this is not right, but I can't think of anything I may be doing wrong. The expected output is to have "password" and "username" strings as the output of golang server.