Skip to content

Commit

Permalink
accept only json for api
Browse files Browse the repository at this point in the history
  • Loading branch information
gernest committed Feb 8, 2024
1 parent d21abc7 commit 18fea2e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func New(ctx context.Context, o *v1.Config) (*API, error) {
return
}
}
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
request.Error(r.Context(), w, http.StatusNotFound, http.StatusText(http.StatusNotFound))
})

a.hand = base
Expand Down
4 changes: 4 additions & 0 deletions request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func Get(ctx context.Context) *protovalidate.Validator {

func Read(w http.ResponseWriter, r *http.Request, o proto.Message) bool {
ctx := r.Context()
if r.Header.Get("Content-Type") != "application/json" {
Error(ctx, w, http.StatusBadRequest, http.StatusText(http.StatusBadRequest))
return false
}
if r.ContentLength == 0 || r.ContentLength > maxBodySize {
logger.Get(ctx).Error("Invalid content length", "contentLength", r.ContentLength)
Error(ctx, w, http.StatusBadRequest, http.StatusText(http.StatusBadRequest))
Expand Down

0 comments on commit 18fea2e

Please sign in to comment.