本文整理汇总了Golang中github.com/ardanlabs/kit/web.Context.Header方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Header方法的具体用法?Golang Context.Header怎么用?Golang Context.Header使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/ardanlabs/kit/web.Context
的用法示例。
在下文中一共展示了Context.Header方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Download
// Download retrieves a set of FormSubmission's based on the search params
// provided in the query string and generates a CSV with the replies.
// 200 Success, 400 Bad Request, 404 Not Found, 500 Internal
func (formSubmissionHandle) Download(c *web.Context) error {
if c.Request.URL.Query().Get("download") != "true" {
// Returns a URL To the CSV file.
results := submission.SearchResults{}
results.CSVURL = fmt.Sprintf("http://%v%v?download=true", c.Request.Host, c.Request.URL.Path)
c.Respond(results, http.StatusOK)
return nil
}
// Generates and returns the CSV file.
// It will only arrive to this handler if the formID exists.
formID := c.Params["form_id"]
var (
limit int
skip int
)
opts := submission.SearchOpts{
Query: c.Request.URL.Query().Get("search"),
FilterBy: c.Request.URL.Query().Get("filterby"),
}
if c.Request.URL.Query().Get("orderby") == "dsc" {
opts.DscOrder = true
}
results, err := submission.Search(c.SessionID, c.Ctx["DB"].(*db.DB), formID, limit, skip, opts)
if err != nil {
return err
}
// Convert into [][]string to encode the CSV.
csvData, err := encodeSubmissionsToCSV(results.Submissions)
if err != nil {
return err
}
// Set the content type.
c.Header().Set("Content-Type", "text/csv")
c.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"ask_%s_%s.csv\"", formID, time.Now().String()))
c.WriteHeader(http.StatusOK)
c.Status = http.StatusOK
c.Write(csvData)
return nil
}