httpx: Add new redirect

This commit is contained in:
Tom Wiesing 2022-11-24 14:19:17 +01:00
parent 41d41035e3
commit 8e2d2cce3e
No known key found for this signature in database
5 changed files with 123 additions and 35 deletions

View file

@ -1,6 +1,25 @@
package httpx
import "errors"
import (
"net/http"
)
// ErrNotFound should be returned from any httpx error to indicate that the item was not found
var ErrNotFound = errors.New("httpx: Error 404")
// Response represents a response to an http request.
type Response struct {
ContentType string // defaults to text/plain
Body []byte
StatusCode int // defaults to [http.StatusOK]
}
func (response Response) ServerHTTP(w http.ResponseWriter, r *http.Request) {
if response.ContentType == "" {
response.ContentType = "text/plain"
}
w.Header().Set("Content-Type", response.ContentType)
if response.StatusCode <= 0 {
response.StatusCode = http.StatusOK
}
w.WriteHeader(response.StatusCode)
w.Write(response.Body)
}