JRehkemper.de

POST Request Status 422 - Unprocessable Entity

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")
profile picture of the author

Jannik Rehkemper

I'm an professional Linux Administrator and Hobby Programmer. My training as an IT-Professional started in 2019 and ended in 2022. Since 2023 I'm working as an Linux Administrator.