From 71a5518d21301fd4a9db5b5ed9065f321133dd42 Mon Sep 17 00:00:00 2001 From: KUNAL KUMAR Date: Thu, 15 Jan 2026 23:34:08 +0530 Subject: [PATCH] fix: UserData struct, register a handler function for an HTTP POST request to the path /json --- cmd/server/main.go | 10 ++++++---- cmd/server/main_test.go | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 6ec165d..0200c3d 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -11,7 +11,9 @@ import ( ) type UserData struct { - Name string + FirstName string + LastName string + Email string } func main() { @@ -22,7 +24,7 @@ func main() { mux.HandleFunc("/hello/", handleHelloParameterized) mux.HandleFunc("/responses/{user}/hello/", handleUserResponsesHello) mux.HandleFunc("/user/hello", handleHelloHeader) - mux.HandleFunc("/json", handleJSON) + mux.HandleFunc("POST /json", handleJSON) fmt.Println("Listening on port 4000") @@ -119,12 +121,12 @@ func handleJSON(w http.ResponseWriter, r *http.Request) { return } - if reqData.Name == "" { + if reqData.FirstName == "" { http.Error(w, "invalid request body!", http.StatusBadRequest) return } - handleHello(w, reqData.Name) + handleHello(w, reqData.FirstName) } diff --git a/cmd/server/main_test.go b/cmd/server/main_test.go index 4b67931..2bdd88a 100644 --- a/cmd/server/main_test.go +++ b/cmd/server/main_test.go @@ -164,7 +164,7 @@ func TestHandleHelloNoHeader(t *testing.T) { func TestHandleJSON(t *testing.T) { testRequest := UserData{ - Name: "human", + FirstName: "human", } marshalledRequestBody, err := json.Marshal(testRequest) @@ -213,7 +213,7 @@ func TestHandleJSONEmptyBody(t *testing.T) { func TestHandleJSONEmptyNameFeild(t *testing.T) { testRequest := UserData{ - Name: "", + FirstName: "", } marshalledRequestBody, err := json.Marshal(testRequest)