Easy JSON responses for Go on top of net/http
go get github.com/gompact/jopherimport Jopher to your project
import (
..."github.com/gompact/jopher"
)There is no need to initialize any thing, the usage is very simple, inside if your handlers use jopher to send a json response
e.g:
funcfooHandler(w http.ResponseWriter, r*http.Request) {
name:="Foo"jopher.Success(w, name)
}The main function Jopher uses to send responses is Write(), you can use it for all of your responses, but alongside it, Jopher provides two other types of methods, error responses, and success responses.
funcfooHandler(w http.ResponseWriter, r*http.Request) {
name:="Foo"jopher.Write(w, http.StatusOK, name)
// orjopher.Write(w, 200, name)
}Success() returns a 200 response with the supplied message as the body of the response
funcfooHandler(w http.ResponseWriter, r*http.Request) {
name:="Foo"jopher.Success(w, name)
}Success() returns a 201 response with the supplied message as the body of the response
funcfooHandler(w http.ResponseWriter, r*http.Request) {
typepoststruct {
TitlestringBodystring
}
// e.g: save post to db: db.Insert(&Post{"foo title", "bar body"})
jopher.Created(w, "Successfully created the new post")
}Error() returns an error response with the supplied message as the body of the response and the a status code.
funcfooHandler(w http.ResponseWriter, r*http.Request) {
typepoststruct {
TitlestringBodystring
}
// e.g: save post to db: err:=db.Insert(&Post{"foo title", "bar body"})
iferr!=nil {
jopher.Error(w, 500, err)
return
}
...
}BadRequest() uses Error() to returns an error response with the supplied message as the body of the response and a status code of 400.
funcfooHandler(w http.ResponseWriter, r*http.Request) {
typeuserstruct {
EmailstringPasswordstring
}
...// e.g: validate request body parametersifreq_body.Get("email") ==nil {
jopher.BadRequest(w, errors.New("Email field is required"))
return
}
}Unauthorized() uses Error() to returns an error response with the supplied message as the body of the response and a status code of 401.
funcfooHandler(w http.ResponseWriter, r*http.Request) {
typeUserstruct {
EmailstringPasswordstring
}
// e.g: save post to db: db.Insert(&Post{"foo title", "bar body"})
ifuser==nil {
jopher.Unauthorized(w, errors.New("You dont have permission to create a new post"))
return
}
...
}NotFound() uses Error() to returns an error response with the supplied message as the body of the response and a status code of 404.
funcfooHandler(w http.ResponseWriter, r*http.Request) {
typeuserstruct {
EmailstringPasswordstring
}
// e.g: trying to find user in dbc, _:=db.Count("users", &user{"foo@bar.com"})
ifc<1 {
jopher.NotFound(w, errors.New("User was not found"))
return
}
...
}InternalServerError() uses Error() to returns an error response with the supplied message as the body of the response and a status code of 500.
funcfooHandler(w http.ResponseWriter, r*http.Request) {
typepoststruct {
TitlestringBodystring
}
// e.g: save post to db: err:=db.Insert(&Post{"foo title", "bar body"})
iferr!=nil {
jopher.InternalServerError(w, err)
return
}
...
}