Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JustSteveKing/go-api-problem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

6 Commits

Repository files navigation

Go API Problem

Go

A simple struct in GoLang for creating API Problems in your API.

Install

$ go get -u github.com/JustSteveKing/go-api-problem

Usage

You use this very simply like:

type server struct{}
func main() {
 s := &server{}
 http.Handle("/", func(rw http.ResponseWriter, r *http.Request) {
 if dbc := db.Where("email = ?", "user@email.com").First(&user); dbc.Error != nil {
 respondWithJSON(
 rw,
 http.StatusBadRequest,
 &APIProblem{
 Title: "Invalid Credentials",
 Detail: "Unable to verify user credentials",
 Status: strconv.Itoa(http.StatusBadRequest),
 Code: "IDEN-001-001",
 },
 "application/problem+json",
 )
 return
 }
 })
 log.Fatal(http.ListenAndServe(":8080", nil))
}
func respondWithJSON(rw http.ResponseWriter, code int, payload interface{}, contentType string) {
	response, _ := json.Marshal(payload)
	rw.Header().Set("Content-Type", contentType)
	rw.WriteHeader(code)
	rw.Write(response)
}

About

A simple struct in GoLang for creating API Problems in your API

Resources

Stars

Watchers

Forks

Packages

Contributors

Languages

AltStyle によって変換されたページ (->オリジナル) /