Documentation ¶
Index ¶
- func Error(w http.ResponseWriter, r *http.Request, err error, status int) error
- func Read(r *http.Request, body interface{}) error
- func Status(w http.ResponseWriter, r *http.Request, status int) error
- func Write(w http.ResponseWriter, r *http.Request, body interface{}, status int) (err error)
- type Err
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Error ¶
Error is used for when there is any kind of error and returns a consistant error format that the user can parse back.
func Read ¶
Read is a helper that responds parses the req.Body into the interface of your choice so that you can actually do something useful with the information that is sent to you.
Example ¶
package main import ( "log" "net/http" "github.com/apriendeau/shttp" ) func main() { // Simple mimic server handler := func(w http.ResponseWriter, r *http.Request) { // parsing for example sake body := make(map[string]interface{}) if err := shttp.Read(r, &body); err != nil { log.Panic(err) } // send body back to mimic if err := shttp.Write(w, r, body, 200); err != nil { log.Panic(err) } } http.HandleFunc("/post", handler) if err := http.ListenAndServe(":8000", nil); err != nil { log.Panic(err) } }
Output:
func Write ¶
Write is what you use to to respond to the req when there is no error. It has content negotiation for xml and json so that people can choose their preferred data format.
Example ¶
package main import ( "log" "net/http" "github.com/apriendeau/shttp" ) func main() { handler := func(w http.ResponseWriter, r *http.Request) { message := struct { Message string `json:"message"` }{"I received your request."} if err := shttp.Write(w, r, message, 200); err != nil { log.Panic(err) } } http.HandleFunc("/post", handler) if err := http.ListenAndServe(":8000", nil); err != nil { log.Panic(err) } }
Output:
Types ¶
type Err ¶
type Err struct { Title string `json:"title" xml:"title"` Description string `json:"description,omitempty" xml:"description,omitempty"` Stack string `json:"stack,omitempty" xml:"stack,omitempty"` Err error `json:"error" xml:"error"` }
Err implements the error interface but also has a couple other fields including stack traces so you can see where exactly the error came from in the API when debugging.
Click to show internal directories.
Click to hide internal directories.