Documentation ¶
Overview ¶
Package httpreq for saving http.Request files
Index ¶
- func BestAcceptMatch(supported, accept string) (string, error)
- func CreateFormFile(w *multipart.Writer, fieldname, filename, contentType string) (io.Writer, error)
- func GetCombinedLogLine(r *http.Request, ts time.Time, status int, size int) string
- func ParseAccept(accept string) (mediaRanges []mediaRange, err error)
- func ReadRequestFiles(r *http.Request) (filenames []string, status int, err error)
- func ReadRequestOneFile(r *http.Request) (body io.ReadCloser, contentType string, status int, err error)
- func SendFile(w http.ResponseWriter, filename, contentType string) error
- func SetLogger(lgr logr.Logger)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BestAcceptMatch ¶
BestAcceptMatch returns the best match between supported media types and accepted media ranges
func CreateFormFile ¶
func CreateFormFile(w *multipart.Writer, fieldname, filename, contentType string) (io.Writer, error)
CreatFormFile is like multipart.Writer.CreateFormFile, but allows the setting of Content-Type.
Example ¶
url, name, contentType := "http://example.com", "filename.pdf", "application/pdf" r := io.Reader(strings.NewReader("%PDF-1.4")) // store entire content of the provided io.Reader in memory var buf bytes.Buffer mw := multipart.NewWriter(&buf) if contentType == "" { contentType = "application/octet-stream" } // "upfile" will be the file's ID (field name) w, err := CreateFormFile(mw, "upfile", name, contentType) if err != nil { panic(err) } if _, err = io.Copy(w, r); err != nil { panic(err) } if err = mw.Close(); err != nil { panic(err) } req, err := http.NewRequest("POST", url, bytes.NewReader(buf.Bytes())) if err != nil { panic(err) } // this is essential: this dresses up our request properly as multipart/form-data req.Header.Set("Content-Type", mw.FormDataContentType()) resp, err := http.DefaultClient.Do(req) if err != nil { panic(err) } if resp.StatusCode >= 300 { panic(errors.New("bad response: " + resp.Status)) }
Output:
func GetCombinedLogLine ¶
GetCombinedLogLine returns a CombinedLog - format of the request copied from https://github.com/gorilla/handlers/blob/master/handlers.go
func ParseAccept ¶
ParseAccept parses the HTTP requests' "Accept" header - see RFC2616
Content-Type = "Content-Type" ":" media-type media-type = type "/" subtype *( ";" parameter ) parameter = attribute "=" value attribute = token value = token | quoted-string quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) qdtext = <any TEXT except <">> quoted-pair = "\" CHAR type = token subtype = token token = 1*<any CHAR except CTLs or separators> separators = "(" | ")" | "<" | ">" | "@"
| "," | ";" | ":" | "\" | <"> | "/" | "[" | "]" | "?" | "=" | "{" | "}" | SP | HT
CTL = <any US-ASCII ctl chr (0-31) and DEL (127)>
func ReadRequestFiles ¶
ReadRequestFiles reads the files from the request, and calls ReaderToFile on them
func ReadRequestOneFile ¶
func ReadRequestOneFile(r *http.Request) (body io.ReadCloser, contentType string, status int, err error)
ReadRequestOneFile reads the first file from the request (if multipart/), or returns the body if not
Types ¶
This section is empty.