httpreq

package
v0.27.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package httpreq for saving http.Request files

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BestAcceptMatch

func BestAcceptMatch(supported, accept string) (string, error)

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

func GetCombinedLogLine(r *http.Request, ts time.Time, status int, size int) string

GetCombinedLogLine returns a CombinedLog - format of the request copied from https://github.com/gorilla/handlers/blob/master/handlers.go

func ParseAccept

func ParseAccept(accept string) (mediaRanges []mediaRange, err error)

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

func ReadRequestFiles(r *http.Request) (filenames []string, status int, err error)

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

func SendFile

func SendFile(w http.ResponseWriter, filename, contentType string) error

SendFile sends the given file as response

func SetLogger added in v0.20.2

func SetLogger(lgr logr.Logger)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL