Skip to content

Commit

Permalink
- Renamed MimeType to MIMETypeJSON.
Browse files Browse the repository at this point in the history
- Added example of output in README.md
  • Loading branch information
shelleeboo committed Oct 15, 2024
1 parent a5729e2 commit 8be0c2f
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/Evernorth/http-problemdetails-go.svg)](https://pkg.go.dev/github.com/Evernorth/http-problemdetails-go)
[![Go Report Card](https://goreportcard.com/badge/github.com/Evernorth/http-problemdetails-go)](https://goreportcard.com/report/github.com/Evernorth/http-problemdetails-go)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![Release](https://img.shields.io/github/v/release/Evernorth/http-problemdetails-go)](https://gtihub.com/Evernorth/http-problemdetails-go/releases)

## Description
An implementation of [IETF RFC 9457 Problem Details for HTTP APIs](https://www.rfc-editor.org/rfc/rfc9457.html) which is a specification for a standard error structure for HTTP APIs.
Expand All @@ -29,7 +28,7 @@ An implementation of [IETF RFC 9457 Problem Details for HTTP APIs](https://www.r
| 504 Gateway Timeout | [`NewGatewayTimeout()`](problemdetails/problemdetails.go) |


>Note: MIME type must be set in the response header to comply with the RFC 9457 specification. The MimeType constant is provided for this purpose.
>Note: MIME type must be set in the response header to comply with the RFC 9457 specification. The `MIMETypeJSON` constant is provided for this purpose.
## Usage
### Creating a simple HTTP ProblemDetails
Expand All @@ -52,7 +51,7 @@ func handler(httpRespWriter http.ResponseWriter, httpReq *http.Request) {
// If the request is not a GET, return a 405 Method Not Allowed
if httpReq.Method != http.MethodGet {
httpRespWriter.Header().Set("Content-Type", problemdetails.MimeType)
httpRespWriter.Header().Set("Content-Type", problemdetails.MIMETypeJSONJSON)
httpRespWriter.WriteHeader(http.StatusMethodNotAllowed)
render.JSON(httpRespWriter, httpReq, problemdetails.NewMethodNotAllowed())
return
Expand All @@ -65,14 +64,14 @@ func handler(httpRespWriter http.ResponseWriter, httpReq *http.Request) {
if exampleType == "basic" {
// Return a 500 Internal Server Error, using the NewInternalServerError function.
httpRespWriter.Header().Set("Content-Type", problemdetails.MimeType)
httpRespWriter.Header().Set("Content-Type", problemdetails.MIMETypeJSON)
httpRespWriter.WriteHeader(http.StatusInternalServerError)
render.JSON(httpRespWriter, httpReq, problemdetails.NewInternalServerError())
} else if exampleType == "advanced" {
// Return a 500 Internal Server Error, using the NewInternalServerError and With* functions.
httpRespWriter.Header().Set("Content-Type", problemdetails.MimeType)
httpRespWriter.Header().Set("Content-Type", problemdetails.MIMETypeJSON)
httpRespWriter.WriteHeader(http.StatusInternalServerError)
render.JSON(httpRespWriter, httpReq, problemdetails.NewInternalServerError().
WithTitle("KABOOM!!!").
Expand All @@ -83,7 +82,7 @@ func handler(httpRespWriter http.ResponseWriter, httpReq *http.Request) {
} else {
// Return a 400 Bad Request, using the NewBadRequest function.
httpRespWriter.Header().Set("Content-Type", problemdetails.MimeType)
httpRespWriter.Header().Set("Content-Type", problemdetails.MIMETypeJSON)
httpRespWriter.WriteHeader(http.StatusBadRequest)
render.JSON(httpRespWriter, httpReq, problemdetails.NewBadRequest().
WithDetail("example-type must be basic or advanced."))
Expand All @@ -102,6 +101,23 @@ func main() {
}
```

Here is an example of the output from the above code:
```
HTTP/1.1 400 Bad Request
Content-Type: application/problem+json
Date: Tue, 15 Oct 2024 16:01:11 GMT
Content-Length: 263
{
"type": "urn:problems:bad-request",
"status": 400,
"title": "Request could not be processed because it is invalid.",
"detail": "example-type must be basic or advanced.",
"instance": "urn:uuid:d9c65ded-ec53-464c-8b3a-be3e8c25bf67",
"created": "2024-10-15T16:01:11.601188Z"
}
```

## Dependencies
See the [go.mod](go.mod) file.

Expand Down

0 comments on commit 8be0c2f

Please sign in to comment.