Skip to content

Commit

Permalink
Fix content-type header in /status response
Browse files Browse the repository at this point in the history
Fixes #20
  • Loading branch information
sharat87 committed Apr 21, 2024
1 parent 21f9be9 commit af040d2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 9 additions & 0 deletions response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package response

import (
"fmt"
"github.com/sharat87/httpbun/util"
"net/http"
)

Expand All @@ -19,6 +20,14 @@ func New(status int, header http.Header, body []byte) Response {
}
}

func JSON(status int, header http.Header, body map[string]any) Response {
if header == nil {
header = http.Header{}
}
header.Set("Content-Type", "application/json")
return New(status, header, util.ToJsonMust(body))
}

func BadRequest(message string, vars ...any) Response {
return New(http.StatusBadRequest, nil, []byte(fmt.Sprintf(message, vars...)))
}
5 changes: 2 additions & 3 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,16 @@ func handleStatus(ex *exchange.Exchange) response.Response {
status = codes[0]
}

ex.ResponseWriter.WriteHeader(status)
acceptHeader := ex.HeaderValueLast("Accept")

if acceptHeader == c.TextPlain {
return response.New(status, nil, []byte(http.StatusText(status)))

} else {
return response.New(status, nil, util.ToJsonMust(map[string]any{
return response.JSON(status, nil, map[string]any{
"code": status,
"description": http.StatusText(status),
}))
})

}
}
Expand Down

0 comments on commit af040d2

Please sign in to comment.