Problem
I want to send a POST Request to my Backend with a JSON-Object in the Body, but the Backend always answers with
422 Unprocessable Entity
Solution
Declare Content-Type JSON in the Request Header.
/* preprocesss data to send in Post Request */
data, err := json.Marshal(server)
if err != nil {
log.Println(err)
}
/* Create POST Requst */
req, err := http.NewRequest("POST", os.Getenv("BACKEND_URL")+"/api/v1/add-server", bytes.NewBuffer(data))
if err != nil {
log.Println(err)
}
/* IMPORTANT: Add JSON Header -> Otherwise the Backend will not convert the json to an object for the database */
req.Header.Add("Content-Type", "application/json")