Skip to content

go-carrot/response

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Carrot

Response

Build Status codecov

Response is a library that generates output that follows carrot/restful-api-spec.

Output Interface

The output conforms to this structure:

{
  "meta": {
    "success": true,
    "status_code": 200,
    "status_text": "OK",
    "error_details": "Invalid email"
  },
  "content": {
    // ...
  }
}

Usage

Dig into response.go for specifics, but this code sample covers all usage:

func (c *MyController) Index(w http.ResponseWriter, r *http.Request) {
    resp := response.New(w)
    defer resp.Output()

    models, err := getModels()
    if err != nil {
        resp.SetErrorDetails(err.Error())
        resp.setResult(http.StatusInternalServerError, nil)
        return
    }

    resp.SetResult(http.StatusOK, models)
}

License

MIT