REST API: add location header to 201 responses

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino
2022-12-23 13:08:04 +01:00
parent ed949604d3
commit e5a8220b8a
8 changed files with 63 additions and 36 deletions

View File

@@ -17,6 +17,7 @@ package httpd
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/go-chi/jwtauth/v5"
@@ -65,12 +66,13 @@ func renderAdmin(w http.ResponseWriter, r *http.Request, username string, status
func addAdmin(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
claims, err := getTokenClaims(r)
if err != nil || claims.Username == "" {
sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
return
}
var admin dataprovider.Admin
admin := dataprovider.Admin{}
err = render.DecodeJSON(r.Body, &admin)
if err != nil {
sendAPIResponse(w, r, err, "", http.StatusBadRequest)
@@ -81,6 +83,7 @@ func addAdmin(w http.ResponseWriter, r *http.Request) {
sendAPIResponse(w, r, err, "", getRespStatus(err))
return
}
w.Header().Add("Location", fmt.Sprintf("%s/%s", adminPath, admin.Username))
renderAdmin(w, r, admin.Username, http.StatusCreated)
}