├── LICENSE ├── jsonapi_test.go ├── README.md └── jsonapi.go /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018-present Lansana Camara (https://github.com/lansana). 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /jsonapi_test.go: -------------------------------------------------------------------------------- 1 | package jsonapi 2 | 3 | import ( 4 | "encoding/json" 5 | "net/http" 6 | "net/http/httptest" 7 | "testing" 8 | ) 9 | 10 | func TestCustomStatusCodeWithNoDataRespond(t *testing.T) { 11 | w := httptest.NewRecorder() 12 | 13 | Respond(w, http.StatusForbidden) 14 | 15 | statusCode := w.Result().StatusCode 16 | if statusCode != http.StatusForbidden { 17 | t.Errorf("expected status code %#v, got %#v", http.StatusForbidden, statusCode) 18 | } 19 | 20 | resp := &Response{} 21 | if err := json.NewDecoder(w.Body).Decode(resp); err != nil { 22 | t.Errorf("expected %#v, got %#v", nil, err) 23 | } 24 | if resp.Data != http.StatusText(statusCode) { 25 | t.Errorf("expected %#v, got %#v", http.StatusText(statusCode), resp.Data) 26 | } 27 | } 28 | 29 | func TestCustomDataRespond(t *testing.T) { 30 | w := httptest.NewRecorder() 31 | data := `{"foo": "bar"}` 32 | 33 | Respond(w, http.StatusOK, data) 34 | 35 | resp := &Response{} 36 | if err := json.NewDecoder(w.Body).Decode(resp); err != nil { 37 | t.Errorf("Expected to get %#v, got %#v", nil, err) 38 | } 39 | if resp.Data != data { 40 | t.Errorf("Expected to get %#v, got %#v", data, resp.Data) 41 | } 42 | } 43 | 44 | func TestExplicitNilDataRespond(t *testing.T) { 45 | w := httptest.NewRecorder() 46 | 47 | Respond(w, http.StatusOK, nil) 48 | 49 | resp := &Response{} 50 | if err := json.NewDecoder(w.Body).Decode(resp); err != nil { 51 | t.Errorf("Expected to get %#v, got %#v", nil, err) 52 | } 53 | if resp.Data != nil { 54 | t.Errorf("Expected to get %#v, got %#v", nil, resp.Data) 55 | } 56 | } 57 | 58 | func TestPanicRespond(t *testing.T) { 59 | w := httptest.NewRecorder() 60 | 61 | defer func() { 62 | err := recover() 63 | if _, ok := err.(*json.UnsupportedTypeError); !ok { 64 | t.Errorf("Expected to get error from recover json.UnsupportedTypeError, got %#v", err) 65 | } 66 | }() 67 | 68 | Respond(w, http.StatusNotFound, map[bool]string{ 69 | true: "", 70 | }) 71 | } 72 | 73 | func TestStandardHTTPResponds(t *testing.T) { 74 | for _, test := range []struct { 75 | f func(w http.ResponseWriter, data ...interface{}) 76 | code int 77 | }{ 78 | {f: Continue, code: http.StatusContinue}, 79 | {f: SwitchingProtocols, code: http.StatusSwitchingProtocols}, 80 | {f: Processing, code: http.StatusProcessing}, 81 | 82 | {f: OK, code: http.StatusOK}, 83 | {f: Created, code: http.StatusCreated}, 84 | {f: Accepted, code: http.StatusAccepted}, 85 | {f: NonAuthoritativeInfo, code: http.StatusNonAuthoritativeInfo}, 86 | {f: NoContent, code: http.StatusNoContent}, 87 | {f: ResetContent, code: http.StatusResetContent}, 88 | {f: PartialContent, code: http.StatusPartialContent}, 89 | {f: MultiStatus, code: http.StatusMultiStatus}, 90 | {f: AlreadyReported, code: http.StatusAlreadyReported}, 91 | {f: IMUsed, code: http.StatusIMUsed}, 92 | 93 | {f: MultipleChoices, code: http.StatusMultipleChoices}, 94 | {f: MovedPermanently, code: http.StatusMovedPermanently}, 95 | {f: Found, code: http.StatusFound}, 96 | {f: SeeOther, code: http.StatusSeeOther}, 97 | {f: NotModified, code: http.StatusNotModified}, 98 | {f: UseProxy, code: http.StatusUseProxy}, 99 | {f: TemporaryRedirect, code: http.StatusTemporaryRedirect}, 100 | {f: PermanentRedirect, code: http.StatusPermanentRedirect}, 101 | 102 | {f: BadRequest, code: http.StatusBadRequest}, 103 | {f: Unauthorized, code: http.StatusUnauthorized}, 104 | {f: PaymentRequired, code: http.StatusPaymentRequired}, 105 | {f: Forbidden, code: http.StatusForbidden}, 106 | {f: NotFound, code: http.StatusNotFound}, 107 | {f: MethodNotAllowed, code: http.StatusMethodNotAllowed}, 108 | {f: NotAcceptable, code: http.StatusNotAcceptable}, 109 | {f: ProxyAuthRequired, code: http.StatusProxyAuthRequired}, 110 | {f: RequestTimeout, code: http.StatusRequestTimeout}, 111 | {f: Conflict, code: http.StatusConflict}, 112 | {f: Gone, code: http.StatusGone}, 113 | {f: LengthRequired, code: http.StatusLengthRequired}, 114 | {f: PreconditionFailed, code: http.StatusPreconditionFailed}, 115 | {f: RequestEntityTooLarge, code: http.StatusRequestEntityTooLarge}, 116 | {f: RequestURITooLong, code: http.StatusRequestURITooLong}, 117 | {f: UnsupportedMediaType, code: http.StatusUnsupportedMediaType}, 118 | {f: RequestedRangeNotSatisfiable, code: http.StatusRequestedRangeNotSatisfiable}, 119 | {f: ExpectationFailed, code: http.StatusExpectationFailed}, 120 | {f: Teapot, code: http.StatusTeapot}, 121 | {f: UnprocessableEntity, code: http.StatusUnprocessableEntity}, 122 | {f: Locked, code: http.StatusLocked}, 123 | {f: FailedDependency, code: http.StatusFailedDependency}, 124 | {f: UpgradeRequired, code: http.StatusUpgradeRequired}, 125 | {f: PreconditionRequired, code: http.StatusPreconditionRequired}, 126 | {f: TooManyRequests, code: http.StatusTooManyRequests}, 127 | {f: RequestHeaderFieldsTooLarge, code: http.StatusRequestHeaderFieldsTooLarge}, 128 | {f: UnavailableForLegalReasons, code: http.StatusUnavailableForLegalReasons}, 129 | 130 | {f: InternalServerError, code: http.StatusInternalServerError}, 131 | {f: NotImplemented, code: http.StatusNotImplemented}, 132 | {f: BadGateway, code: http.StatusBadGateway}, 133 | {f: ServiceUnavailable, code: http.StatusServiceUnavailable}, 134 | {f: GatewayTimeout, code: http.StatusGatewayTimeout}, 135 | {f: HTTPVersionNotSupported, code: http.StatusHTTPVersionNotSupported}, 136 | {f: VariantAlsoNegotiates, code: http.StatusVariantAlsoNegotiates}, 137 | {f: InsufficientStorage, code: http.StatusInsufficientStorage}, 138 | {f: LoopDetected, code: http.StatusLoopDetected}, 139 | {f: NotExtended, code: http.StatusNotExtended}, 140 | {f: NetworkAuthenticationRequired, code: http.StatusNetworkAuthenticationRequired}, 141 | } { 142 | w := httptest.NewRecorder() 143 | test.f(w) 144 | if w.Result().StatusCode != test.code { 145 | t.Errorf("Expected to get %#v, got %#v", w.Result().StatusCode, test.code) 146 | } 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jsonapi 2 | A lightweight JSON response writer for Go. Simple, fully tested, expressive and uses only the standard library. 3 | 4 | ## Examples 5 | 6 | The `data` argument is optional on all methods. If omitted, the response data field will be set to the HTTP status text. If provided, the response data field will be set to the first argument, and all other arguments will be ignored. This allows for optional arguments with default values without requiring any configuration or structs. 7 | 8 | **Action:** 9 | 10 | ```go 11 | func (w http.ResponseWriter, r *http.Request) { 12 | jsonapi.OK(w) 13 | } 14 | ``` 15 | 16 | **Result:** 17 | 18 | ```go 19 | 200 OK 20 | {"code": 200, "data": "OK"} 21 | ``` 22 | 23 | **Action:** 24 | 25 | ```go 26 | func (w http.ResponseWriter, r *http.Request) { 27 | jsonapi.OK(w, map[string]string{"foo": "bar"}) 28 | } 29 | ``` 30 | 31 | **Result:** 32 | 33 | ```go 34 | 200 OK 35 | {"code": 200, "data": {"foo": "bar"}} 36 | ``` 37 | 38 | **Action:** 39 | 40 | ```go 41 | func (w http.ResponseWriter, r *http.Request) { 42 |    type User struct { 43 | Name string `json:"name"` 44 | Email string `json:"email" 45 | } 46 | jsonapi.Created(w, User{Name: "John Doe", email: "johndoe@domain.com") 47 | } 48 | ``` 49 | 50 | **Result:** 51 | 52 | ```go 53 | 201 Created 54 | {"code": 201, "data": {"name": "John Doe", "email", "johndoe@domain.com"}} 55 | ``` 56 | 57 | ## Responder interface 58 | 59 | The interface contains methods for writing all standardized HTTP response codes provided by the standard library. 60 | 61 | ```go 62 | // Responder is an interface for writing standardized JSON responses. 63 | type Responder interface { 64 | Respond(w http.ResponseWriter, status int, data ...interface{}) 65 | 66 | // 100 status codes. 67 | Continue(w http.ResponseWriter, data ...interface{}) 68 | SwitchingProtocols(w http.ResponseWriter, data ...interface{}) 69 | Processing(w http.ResponseWriter, data ...interface{}) 70 | 71 | // 200 status codes. 72 | OK(w http.ResponseWriter, data ...interface{}) 73 | Created(w http.ResponseWriter, data ...interface{}) 74 | Accepted(w http.ResponseWriter, data ...interface{}) 75 | NonAuthoritativeInfo(w http.ResponseWriter, data ...interface{}) 76 | NoContent(w http.ResponseWriter, data ...interface{}) 77 | ResetContent(w http.ResponseWriter, data ...interface{}) 78 | PartialContent(w http.ResponseWriter, data ...interface{}) 79 | MultiStatus(w http.ResponseWriter, data ...interface{}) 80 | AlreadyReported(w http.ResponseWriter, data ...interface{}) 81 | IMUsed(w http.ResponseWriter, data ...interface{}) 82 | 83 | // 300 status codes. 84 | MultipleChoices(w http.ResponseWriter, data ...interface{}) 85 | MovedPermanently(w http.ResponseWriter, data ...interface{}) 86 | Found(w http.ResponseWriter, data ...interface{}) 87 | SeeOther(w http.ResponseWriter, data ...interface{}) 88 | NotModified(w http.ResponseWriter, data ...interface{}) 89 | UseProxy(w http.ResponseWriter, data ...interface{}) 90 | SwitchProxy(w http.ResponseWriter, data ...interface{}) 91 | TemporaryRedirect(w http.ResponseWriter, data ...interface{}) 92 | PermanentRedirect(w http.ResponseWriter, data ...interface{}) 93 | 94 | // 400 status codes. 95 | BadRequest(w http.ResponseWriter, data ...interface{}) 96 | Unauthorized(w http.ResponseWriter, data ...interface{}) 97 | PaymentRequired(w http.ResponseWriter, data ...interface{}) 98 | Forbidden(w http.ResponseWriter, data ...interface{}) 99 | NotFound(w http.ResponseWriter, data ...interface{}) 100 | MethodNotAllowed(w http.ResponseWriter, data ...interface{}) 101 | NotAcceptable(w http.ResponseWriter, data ...interface{}) 102 | ProxyAuthenticationRequired(w http.ResponseWriter, data ...interface{}) 103 | RequestTimeout(w http.ResponseWriter, data ...interface{}) 104 | Conflict(w http.ResponseWriter, data ...interface{}) 105 | Gone(w http.ResponseWriter, data ...interface{}) 106 | LengthRequired(w http.ResponseWriter, data ...interface{}) 107 | PreconditionFailed(w http.ResponseWriter, data ...interface{}) 108 | PayloadTooLarge(w http.ResponseWriter, data ...interface{}) 109 | URITooLong(w http.ResponseWriter, data ...interface{}) 110 | UnsupportedMediaType(w http.ResponseWriter, data ...interface{}) 111 | RangeNotSatisfiable(w http.ResponseWriter, data ...interface{}) 112 | ExpectationFailed(w http.ResponseWriter, data ...interface{}) 113 | Teapot(w http.ResponseWriter, data ...interface{}) 114 | MisdirectedRequest(w http.ResponseWriter, data ...interface{}) 115 | UnprocessableEntity(w http.ResponseWriter, data ...interface{}) 116 | Locked(w http.ResponseWriter, data ...interface{}) 117 | FailedDependency(w http.ResponseWriter, data ...interface{}) 118 | UpgradeRequired(w http.ResponseWriter, data ...interface{}) 119 | PreconditionRequired(w http.ResponseWriter, data ...interface{}) 120 | TooManyRequests(w http.ResponseWriter, data ...interface{}) 121 | RequestHeaderFieldsTooLarge(w http.ResponseWriter, data ...interface{}) 122 | UnavailableForLegalReasons(w http.ResponseWriter, data ...interface{}) 123 | 124 | // 500 status codes. 125 | InternalServerError(w http.ResponseWriter, data ...interface{}) 126 | NotImplemented(w http.ResponseWriter, data ...interface{}) 127 | BadGateway(w http.ResponseWriter, data ...interface{}) 128 | ServiceUnavailable(w http.ResponseWriter, data ...interface{}) 129 | GatewayTimeout(w http.ResponseWriter, data ...interface{}) 130 | HTTPVersionNotSupported(w http.ResponseWriter, data ...interface{}) 131 | VariantAlsoNegotiates(w http.ResponseWriter, data ...interface{}) 132 | InsufficientStorage(w http.ResponseWriter, data ...interface{}) 133 | LoopDetected(w http.ResponseWriter, data ...interface{}) 134 | NotExtended(w http.ResponseWriter, data ...interface{}) 135 | NetworkAuthenticationRequired(w http.ResponseWriter, data ...interface{}) 136 | } 137 | ``` 138 | 139 | ## License 140 | 141 | Copyright (c) 2018-present [Lansana Camara](https://github.com/lansana) 142 | 143 | Licensed under [MIT License](./LICENSE) 144 | -------------------------------------------------------------------------------- /jsonapi.go: -------------------------------------------------------------------------------- 1 | package jsonapi 2 | 3 | import ( 4 | "encoding/json" 5 | "net/http" 6 | ) 7 | 8 | // Responder is an interface for writing standardized JSON responses. 9 | type Responder interface { 10 | Respond(w http.ResponseWriter, status int, data ...interface{}) 11 | 12 | // 100 status codes. 13 | Continue(w http.ResponseWriter, data ...interface{}) 14 | SwitchingProtocols(w http.ResponseWriter, data ...interface{}) 15 | Processing(w http.ResponseWriter, data ...interface{}) 16 | 17 | // 200 status codes. 18 | OK(w http.ResponseWriter, data ...interface{}) 19 | Created(w http.ResponseWriter, data ...interface{}) 20 | Accepted(w http.ResponseWriter, data ...interface{}) 21 | NonAuthoritativeInfo(w http.ResponseWriter, data ...interface{}) 22 | NoContent(w http.ResponseWriter, data ...interface{}) 23 | ResetContent(w http.ResponseWriter, data ...interface{}) 24 | PartialContent(w http.ResponseWriter, data ...interface{}) 25 | MultiStatus(w http.ResponseWriter, data ...interface{}) 26 | AlreadyReported(w http.ResponseWriter, data ...interface{}) 27 | IMUsed(w http.ResponseWriter, data ...interface{}) 28 | 29 | // 300 status codes. 30 | MultipleChoices(w http.ResponseWriter, data ...interface{}) 31 | MovedPermanently(w http.ResponseWriter, data ...interface{}) 32 | Found(w http.ResponseWriter, data ...interface{}) 33 | SeeOther(w http.ResponseWriter, data ...interface{}) 34 | NotModified(w http.ResponseWriter, data ...interface{}) 35 | UseProxy(w http.ResponseWriter, data ...interface{}) 36 | SwitchProxy(w http.ResponseWriter, data ...interface{}) 37 | TemporaryRedirect(w http.ResponseWriter, data ...interface{}) 38 | PermanentRedirect(w http.ResponseWriter, data ...interface{}) 39 | 40 | // 400 status codes. 41 | BadRequest(w http.ResponseWriter, data ...interface{}) 42 | Unauthorized(w http.ResponseWriter, data ...interface{}) 43 | PaymentRequired(w http.ResponseWriter, data ...interface{}) 44 | Forbidden(w http.ResponseWriter, data ...interface{}) 45 | NotFound(w http.ResponseWriter, data ...interface{}) 46 | MethodNotAllowed(w http.ResponseWriter, data ...interface{}) 47 | NotAcceptable(w http.ResponseWriter, data ...interface{}) 48 | ProxyAuthenticationRequired(w http.ResponseWriter, data ...interface{}) 49 | RequestTimeout(w http.ResponseWriter, data ...interface{}) 50 | Conflict(w http.ResponseWriter, data ...interface{}) 51 | Gone(w http.ResponseWriter, data ...interface{}) 52 | LengthRequired(w http.ResponseWriter, data ...interface{}) 53 | PreconditionFailed(w http.ResponseWriter, data ...interface{}) 54 | PayloadTooLarge(w http.ResponseWriter, data ...interface{}) 55 | URITooLong(w http.ResponseWriter, data ...interface{}) 56 | UnsupportedMediaType(w http.ResponseWriter, data ...interface{}) 57 | RangeNotSatisfiable(w http.ResponseWriter, data ...interface{}) 58 | ExpectationFailed(w http.ResponseWriter, data ...interface{}) 59 | Teapot(w http.ResponseWriter, data ...interface{}) 60 | MisdirectedRequest(w http.ResponseWriter, data ...interface{}) 61 | UnprocessableEntity(w http.ResponseWriter, data ...interface{}) 62 | Locked(w http.ResponseWriter, data ...interface{}) 63 | FailedDependency(w http.ResponseWriter, data ...interface{}) 64 | UpgradeRequired(w http.ResponseWriter, data ...interface{}) 65 | PreconditionRequired(w http.ResponseWriter, data ...interface{}) 66 | TooManyRequests(w http.ResponseWriter, data ...interface{}) 67 | RequestHeaderFieldsTooLarge(w http.ResponseWriter, data ...interface{}) 68 | UnavailableForLegalReasons(w http.ResponseWriter, data ...interface{}) 69 | 70 | // 500 status codes. 71 | InternalServerError(w http.ResponseWriter, data ...interface{}) 72 | NotImplemented(w http.ResponseWriter, data ...interface{}) 73 | BadGateway(w http.ResponseWriter, data ...interface{}) 74 | ServiceUnavailable(w http.ResponseWriter, data ...interface{}) 75 | GatewayTimeout(w http.ResponseWriter, data ...interface{}) 76 | HTTPVersionNotSupported(w http.ResponseWriter, data ...interface{}) 77 | VariantAlsoNegotiates(w http.ResponseWriter, data ...interface{}) 78 | InsufficientStorage(w http.ResponseWriter, data ...interface{}) 79 | LoopDetected(w http.ResponseWriter, data ...interface{}) 80 | NotExtended(w http.ResponseWriter, data ...interface{}) 81 | NetworkAuthenticationRequired(w http.ResponseWriter, data ...interface{}) 82 | } 83 | 84 | // Response is the default JSON structure that will be written. 85 | type Response struct { 86 | Code int `json:"code"` 87 | Data interface{} `json:"data"` 88 | } 89 | 90 | // respond writes a JSON-encoded body to http.ResponseWriter. 91 | // 92 | // he data argument is optional on all methods. If omitted, the response data field 93 | // will be set to the HTTP status text. If provided, the response data field will be 94 | // set to the first argument, and all other arguments will be ignored. 95 | func respond(w http.ResponseWriter, statusCode int, data ...interface{}) { 96 | r := new(Response) 97 | r.Code = statusCode 98 | 99 | if len(data) == 0 { 100 | r.Data = http.StatusText(statusCode) 101 | } else { 102 | r.Data = data[0] 103 | } 104 | 105 | w.Header().Set("Content-Type", "application/json; charset=UTF-8") 106 | w.WriteHeader(statusCode) 107 | 108 | if err := json.NewEncoder(w).Encode(r); err != nil { 109 | panic(err) 110 | } 111 | } 112 | 113 | // Respond writes data with a custom status. 114 | func Respond(w http.ResponseWriter, status int, data ...interface{}) { 115 | respond(w, status, data...) 116 | } 117 | 118 | // Continue writes data with status code 100. 119 | func Continue(w http.ResponseWriter, data ...interface{}) { 120 | respond(w, http.StatusContinue, data...) 121 | } 122 | 123 | // SwitchingProtocols writes data with status code 101. 124 | func SwitchingProtocols(w http.ResponseWriter, data ...interface{}) { 125 | respond(w, http.StatusSwitchingProtocols, data...) 126 | } 127 | 128 | // Processing writes data with status code 102. 129 | func Processing(w http.ResponseWriter, data ...interface{}) { 130 | respond(w, http.StatusProcessing, data...) 131 | } 132 | 133 | // OK writes data with status code 200. 134 | func OK(w http.ResponseWriter, data ...interface{}) { 135 | respond(w, http.StatusOK, data...) 136 | } 137 | 138 | // Created writes data with status code 201. 139 | func Created(w http.ResponseWriter, data ...interface{}) { 140 | respond(w, http.StatusCreated, data...) 141 | } 142 | 143 | // Accepted writes data with status code 202. 144 | func Accepted(w http.ResponseWriter, data ...interface{}) { 145 | respond(w, http.StatusAccepted, data...) 146 | } 147 | 148 | // NonAuthoritativeInfo writes data with status code 203. 149 | func NonAuthoritativeInfo(w http.ResponseWriter, data ...interface{}) { 150 | respond(w, http.StatusNonAuthoritativeInfo, data...) 151 | } 152 | 153 | // NoContent writes data with status code 204. 154 | func NoContent(w http.ResponseWriter, data ...interface{}) { 155 | respond(w, http.StatusNoContent, data...) 156 | } 157 | 158 | // ResetContent writes data with status code 205. 159 | func ResetContent(w http.ResponseWriter, data ...interface{}) { 160 | respond(w, http.StatusResetContent, data...) 161 | } 162 | 163 | // PartialContent writes data with status code 206. 164 | func PartialContent(w http.ResponseWriter, data ...interface{}) { 165 | respond(w, http.StatusPartialContent, data...) 166 | } 167 | 168 | // MultiStatus writes data with status code 207. 169 | func MultiStatus(w http.ResponseWriter, data ...interface{}) { 170 | respond(w, http.StatusMultiStatus, data...) 171 | } 172 | 173 | // AlreadyReported writes data with status code 208. 174 | func AlreadyReported(w http.ResponseWriter, data ...interface{}) { 175 | respond(w, http.StatusAlreadyReported, data...) 176 | } 177 | 178 | // IMUsed writes data with status code 226. 179 | func IMUsed(w http.ResponseWriter, data ...interface{}) { 180 | respond(w, http.StatusIMUsed, data...) 181 | } 182 | 183 | // MultipleChoices writes data with status code 300. 184 | func MultipleChoices(w http.ResponseWriter, data ...interface{}) { 185 | respond(w, http.StatusMultipleChoices, data...) 186 | } 187 | 188 | // MovedPermanently writes data with status code 301. 189 | func MovedPermanently(w http.ResponseWriter, data ...interface{}) { 190 | respond(w, http.StatusMovedPermanently, data...) 191 | } 192 | 193 | // Found writes data with status code 302. 194 | func Found(w http.ResponseWriter, data ...interface{}) { 195 | respond(w, http.StatusFound, data...) 196 | } 197 | 198 | // SeeOther writes data with status code 303. 199 | func SeeOther(w http.ResponseWriter, data ...interface{}) { 200 | respond(w, http.StatusSeeOther, data...) 201 | } 202 | 203 | // NotModified writes data with status code 304. 204 | func NotModified(w http.ResponseWriter, data ...interface{}) { 205 | respond(w, http.StatusNotModified, data...) 206 | } 207 | 208 | // UseProxy writes data with status code 305. 209 | func UseProxy(w http.ResponseWriter, data ...interface{}) { 210 | respond(w, http.StatusUseProxy, data...) 211 | } 212 | 213 | // TemporaryRedirect writes data with status code 307. 214 | func TemporaryRedirect(w http.ResponseWriter, data ...interface{}) { 215 | respond(w, http.StatusTemporaryRedirect, data...) 216 | } 217 | 218 | // PermanentRedirect writes data with status code 308. 219 | func PermanentRedirect(w http.ResponseWriter, data ...interface{}) { 220 | respond(w, http.StatusPermanentRedirect, data...) 221 | } 222 | 223 | // BadRequest writes data with status code 400. 224 | func BadRequest(w http.ResponseWriter, data ...interface{}) { 225 | respond(w, http.StatusBadRequest, data...) 226 | } 227 | 228 | // Unauthorized writes data with status code 401. 229 | func Unauthorized(w http.ResponseWriter, data ...interface{}) { 230 | respond(w, http.StatusUnauthorized, data...) 231 | } 232 | 233 | // PaymentRequired writes data with status code 402. 234 | func PaymentRequired(w http.ResponseWriter, data ...interface{}) { 235 | respond(w, http.StatusPaymentRequired, data...) 236 | } 237 | 238 | // Forbidden writes data with status code 403. 239 | func Forbidden(w http.ResponseWriter, data ...interface{}) { 240 | respond(w, http.StatusForbidden, data...) 241 | } 242 | 243 | // NotFound writes data with status code 404. 244 | func NotFound(w http.ResponseWriter, data ...interface{}) { 245 | respond(w, http.StatusNotFound, data...) 246 | } 247 | 248 | // MethodNotAllowed writes data with status code 405. 249 | func MethodNotAllowed(w http.ResponseWriter, data ...interface{}) { 250 | respond(w, http.StatusMethodNotAllowed, data...) 251 | } 252 | 253 | // NotAcceptable writes data with status code 406. 254 | func NotAcceptable(w http.ResponseWriter, data ...interface{}) { 255 | respond(w, http.StatusNotAcceptable, data...) 256 | } 257 | 258 | // ProxyAuthRequired writes data with status code 407. 259 | func ProxyAuthRequired(w http.ResponseWriter, data ...interface{}) { 260 | respond(w, http.StatusProxyAuthRequired, data...) 261 | } 262 | 263 | // RequestTimeout writes data with status code 408. 264 | func RequestTimeout(w http.ResponseWriter, data ...interface{}) { 265 | respond(w, http.StatusRequestTimeout, data...) 266 | } 267 | 268 | // Conflict writes data with status code 409. 269 | func Conflict(w http.ResponseWriter, data ...interface{}) { 270 | respond(w, http.StatusConflict, data...) 271 | } 272 | 273 | // Gone writes data with status code 410. 274 | func Gone(w http.ResponseWriter, data ...interface{}) { 275 | respond(w, http.StatusGone, data...) 276 | } 277 | 278 | // LengthRequired writes data with status code 411. 279 | func LengthRequired(w http.ResponseWriter, data ...interface{}) { 280 | respond(w, http.StatusLengthRequired, data...) 281 | } 282 | 283 | // PreconditionFailed writes data with status code 412. 284 | func PreconditionFailed(w http.ResponseWriter, data ...interface{}) { 285 | respond(w, http.StatusPreconditionFailed, data...) 286 | } 287 | 288 | // RequestEntityTooLarge writes data with status code 413. 289 | func RequestEntityTooLarge(w http.ResponseWriter, data ...interface{}) { 290 | respond(w, http.StatusRequestEntityTooLarge, data...) 291 | } 292 | 293 | // RequestURITooLong writes data with status code 414. 294 | func RequestURITooLong(w http.ResponseWriter, data ...interface{}) { 295 | respond(w, http.StatusRequestURITooLong, data...) 296 | } 297 | 298 | // UnsupportedMediaType writes data with status code 415. 299 | func UnsupportedMediaType(w http.ResponseWriter, data ...interface{}) { 300 | respond(w, http.StatusUnsupportedMediaType, data...) 301 | } 302 | 303 | // RequestedRangeNotSatisfiable writes data with status code 416. 304 | func RequestedRangeNotSatisfiable(w http.ResponseWriter, data ...interface{}) { 305 | respond(w, http.StatusRequestedRangeNotSatisfiable, data...) 306 | } 307 | 308 | // ExpectationFailed writes data with status code 417. 309 | func ExpectationFailed(w http.ResponseWriter, data ...interface{}) { 310 | respond(w, http.StatusExpectationFailed, data...) 311 | } 312 | 313 | // Teapot writes data with status code 418. 314 | func Teapot(w http.ResponseWriter, data ...interface{}) { 315 | respond(w, http.StatusTeapot, data...) 316 | } 317 | 318 | // UnprocessableEntity writes data with status code 422. 319 | func UnprocessableEntity(w http.ResponseWriter, data ...interface{}) { 320 | respond(w, http.StatusUnprocessableEntity, data...) 321 | } 322 | 323 | // Locked writes data with status code 423. 324 | func Locked(w http.ResponseWriter, data ...interface{}) { 325 | respond(w, http.StatusLocked, data...) 326 | } 327 | 328 | // FailedDependency writes data with status code 424. 329 | func FailedDependency(w http.ResponseWriter, data ...interface{}) { 330 | respond(w, http.StatusFailedDependency, data...) 331 | } 332 | 333 | // UpgradeRequired writes data with status code 426. 334 | func UpgradeRequired(w http.ResponseWriter, data ...interface{}) { 335 | respond(w, http.StatusUpgradeRequired, data...) 336 | } 337 | 338 | // PreconditionRequired writes data with status code 428. 339 | func PreconditionRequired(w http.ResponseWriter, data ...interface{}) { 340 | respond(w, http.StatusPreconditionRequired, data...) 341 | } 342 | 343 | // TooManyRequests writes data with status code 429. 344 | func TooManyRequests(w http.ResponseWriter, data ...interface{}) { 345 | respond(w, http.StatusTooManyRequests, data...) 346 | } 347 | 348 | // RequestHeaderFieldsTooLarge writes data with status code 431. 349 | func RequestHeaderFieldsTooLarge(w http.ResponseWriter, data ...interface{}) { 350 | respond(w, http.StatusRequestHeaderFieldsTooLarge, data...) 351 | } 352 | 353 | // UnavailableForLegalReasons writes data with status code 451. 354 | func UnavailableForLegalReasons(w http.ResponseWriter, data ...interface{}) { 355 | respond(w, http.StatusUnavailableForLegalReasons, data...) 356 | } 357 | 358 | // InternalServerError writes data with status code 500. 359 | func InternalServerError(w http.ResponseWriter, data ...interface{}) { 360 | respond(w, http.StatusInternalServerError, data...) 361 | } 362 | 363 | // NotImplemented writes data with status code 501. 364 | func NotImplemented(w http.ResponseWriter, data ...interface{}) { 365 | respond(w, http.StatusNotImplemented, data...) 366 | } 367 | 368 | // BadGateway writes data with status code 502. 369 | func BadGateway(w http.ResponseWriter, data ...interface{}) { 370 | respond(w, http.StatusBadGateway, data...) 371 | } 372 | 373 | // ServiceUnavailable writes data with status code 503. 374 | func ServiceUnavailable(w http.ResponseWriter, data ...interface{}) { 375 | respond(w, http.StatusServiceUnavailable, data...) 376 | } 377 | 378 | // GatewayTimeout writes data with status code 504. 379 | func GatewayTimeout(w http.ResponseWriter, data ...interface{}) { 380 | respond(w, http.StatusGatewayTimeout, data...) 381 | } 382 | 383 | // HTTPVersionNotSupported writes data with status code 505. 384 | func HTTPVersionNotSupported(w http.ResponseWriter, data ...interface{}) { 385 | respond(w, http.StatusHTTPVersionNotSupported, data...) 386 | } 387 | 388 | // VariantAlsoNegotiates writes data with status code 506. 389 | func VariantAlsoNegotiates(w http.ResponseWriter, data ...interface{}) { 390 | respond(w, http.StatusVariantAlsoNegotiates, data...) 391 | } 392 | 393 | // InsufficientStorage writes data with status code 507. 394 | func InsufficientStorage(w http.ResponseWriter, data ...interface{}) { 395 | respond(w, http.StatusInsufficientStorage, data...) 396 | } 397 | 398 | // LoopDetected writes data with status code 508. 399 | func LoopDetected(w http.ResponseWriter, data ...interface{}) { 400 | respond(w, http.StatusLoopDetected, data...) 401 | } 402 | 403 | // NotExtended writes data with status code 510. 404 | func NotExtended(w http.ResponseWriter, data ...interface{}) { 405 | respond(w, http.StatusNotExtended, data...) 406 | } 407 | 408 | // NetworkAuthenticationRequired writes data with status code 511. 409 | func NetworkAuthenticationRequired(w http.ResponseWriter, data ...interface{}) { 410 | respond(w, http.StatusNetworkAuthenticationRequired, data...) 411 | } 412 | --------------------------------------------------------------------------------