You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28Lines changed: 28 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,6 +73,34 @@ See [here](https://httpstatuses.com/) for a complete list of HTTP responses, alo
73
73
74
74
Please submit a PR if you want to add to this list. Only the most common response types have been included.
75
75
76
+
## To Long, Don't Write
77
+
78
+
Sometimes you don't need to return a specific content-message but don't want the response body to be empty.
79
+
In this case you can use the `DefaultMessage()` for responding with json containing the default message for the corresponding status code.
80
+
81
+
```go
82
+
package main
83
+
84
+
import (
85
+
"net/http"
86
+
resp "github.com/nicklaw5/go-respond"
87
+
)
88
+
89
+
funcmain() {
90
+
http.HandleFunc("/api/users", func(w http.ResponseWriter, r *http.Request) {
91
+
// ...
92
+
if !authenticated {
93
+
resp.NewResponse(w).DefaultMessage().
94
+
Unauthorized(nil)
95
+
}
96
+
// ...
97
+
})
98
+
http.ListenAndServe(":8080", nil)
99
+
}
100
+
```
101
+
102
+
Would respond with `{"status":401,"message":"Unauthorized"}`
103
+
76
104
## Handling Errors
77
105
78
106
The best option for handling errors that may occur while marshalling the JSON response, is to use [Negroni's Recovery middleware](https://github.com/urfave/negroni#recovery). Here's an example:
0 commit comments