dimanche 28 mars 2021

Problems parsing POST request with GO webserver

I am very new to GO and I am trying to set up a very basic GO webserver and so far I have had no issues handling GET requests without any parameters, however now I am trying a POST request with some parameters and I am having two different issues depending on how I send the request.

Here's the code that handles the route

func SubmitData(w http.ResponseWriter, r *http.Request) {
    reqBody, _ := ioutil.ReadAll(r.Body)

    var userInfo SubmittedUserInfo
    fmt.Println(string(reqBody))

    unmarshalErr := json.Unmarshal(reqBody, &userInfo)
    if unmarshalErr != nil {
        fmt.Println("Error Unmarshalling JSON:", unmarshalErr)
    }

    _ = json.NewEncoder(w).Encode(userInfo)

    if unmarshalErr != nil {
        log.Printf("error decoding response: %v", unmarshalErr)
        if e, ok := unmarshalErr.(*json.SyntaxError); ok {
            log.Printf("syntax error at byte offset %d", e.Offset)
        }
        log.Printf("request body: %q", reqBody)
    }

}

Here's the SubmittedUserInfo structure that I am trying to create with the JSON data.

type SubmittedUserInfo struct {
    Number int16 `json:"Number"` // is always a number between 0 and 10
    Age int16 `json:"Age"`
    Nationality string `json:"Nationality"`
    Gender string `json:"Gender"`
    DominantHand string `json:"DominantHand"`
}

Here is the function that handles the requests

func handleRequests(port string) {
    muxRouter := mux.NewRouter().StrictSlash(true)
    muxRouter.HandleFunc("/", Home)
    muxRouter.HandleFunc("/health", HealthCheck)
    muxRouter.HandleFunc("/submit_data", SubmitData).Methods("POST")
    log.Fatal(http.ListenAndServe(port, muxRouter))
}

Here are the actual errors I am getting. If I send the following request with Python 3.8 I get the corresponding error

import requests

url = "http://localhost:5050/submit_data"

args = {
    "number": 0,
    "age": 25,
    "nationality": "russian",
    "gender": "male",
    "dominantHand": "right"
}
response = requests.post(url, args)

Error:

Error Unmarshalling JSON: invalid character 'N' looking for beginning of value
2021/03/28 11:23:27 error decoding response: invalid character 'N' looking for beginning of value
2021/03/28 11:23:27 syntax error at byte offset 1
2021/03/28 11:23:27 request body: "Number=0&Age=25&Nationality=russian&Gender=male&DominantHand=right"

Now if I send a request with Postman specifying the exact same parameters I get the error

Error Unmarshalling JSON: unexpected end of JSON input
2021/03/28 11:24:38 error decoding response: unexpected end of JSON input
2021/03/28 11:24:38 syntax error at byte offset 0
2021/03/28 11:24:38 request body: ""

What am I doing wrong? I am very used to doing this in Python and I won't be surprised if there's some additional work I need to do in GO to ensure everything is formatted properly, but I have no idea what that might be, so any help or advice would be greatly appreciated!




Aucun commentaire:

Enregistrer un commentaire