本文整理匯總了Golang中http.Request類的典型用法代碼示例。如果您正苦於以下問題:Golang Request類的具體用法?Golang Request怎麽用?Golang Request使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Request類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: buildTestRequest
func buildTestRequest(method string, path string, body string, headers map[string][]string, cookies []*http.Cookie) *http.Request {
host := "127.0.0.1"
port := "80"
rawurl := "http://" + host + ":" + port + path
url_, _ := url.Parse(rawurl)
proto := "HTTP/1.1"
if headers == nil {
headers = map[string][]string{}
}
headers["User-Agent"] = []string{"web.go test"}
if method == "POST" {
headers["Content-Length"] = []string{fmt.Sprintf("%d", len(body))}
if headers["Content-Type"] == nil {
headers["Content-Type"] = []string{"text/plain"}
}
}
req := http.Request{Method: method,
RawURL: rawurl,
URL: url_,
Proto: proto,
Host: host,
Header: http.Header(headers),
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
}
for _, cookie := range cookies {
req.AddCookie(cookie)
}
return &req
}
示例2: myWidgets
func myWidgets(w http.ResponseWriter, r *http.Request) {
var err os.Error
ctx := appengine.NewContext(r)
page, err := template.Parse(myWidgetTemplate, nil)
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
data := myWidgetData{
CSS: commonCSS(),
Header: header(ctx),
}
data.Widget, err = LoadWidgets(ctx)
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
if len(r.FormValue("ids_only")) > 0 {
w.Header().Set("Content-Type", "text/plain")
for _, widget := range data.Widget {
fmt.Fprintln(w, widget.ID)
}
return
}
page.Execute(w, data)
}
示例3: handleInboxItem
func handleInboxItem(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
userid, _, err := getSession(c, r)
if err != nil {
sendError(w, r, "No session cookie")
return
}
s := newStore(c)
perma_blobref := r.FormValue("perma")
item, err := s.GetInboxItem(userid, perma_blobref)
if err != nil {
http.Error(w, "Could not get inbox item", http.StatusInternalServerError)
c.Errorf("handleInboxItem: %v", err)
return
}
entry := make(map[string]interface{})
entry["perma"] = perma_blobref
entry["seq"] = item.LastSeq
err = fillInboxItem(s, perma_blobref, item.LastSeq, entry)
if err != nil {
fmt.Fprintf(w, `{"ok":false, "error":%v}`, err.String())
return
}
info, _ := json.Marshal(entry)
fmt.Fprintf(w, `{"ok":true, "item":%v}`, string(info))
}
示例4: search
func search(c *http.Conn, r *http.Request) {
query := strings.TrimSpace(r.FormValue("q"))
var result SearchResult
if index, timestamp := searchIndex.get(); index != nil {
result.Query = query
result.Hit, result.Alt, result.Illegal = index.(*Index).Lookup(query)
_, ts := fsTree.get()
result.Accurate = timestamp >= ts
}
var buf bytes.Buffer
if err := searchHTML.Execute(result, &buf); err != nil {
log.Stderrf("searchHTML.Execute: %s", err)
}
var title string
if result.Hit != nil {
title = fmt.Sprintf(`Results for query %q`, query)
} else {
title = fmt.Sprintf(`No results found for query %q`, query)
}
servePage(c, title, query, buf.Bytes())
}
示例5: handle
func handle(w http.ResponseWriter, r *http.Request) {
// If root, show the link registration page.
if r.URL.Path == "/" {
switch r.Method {
case "GET":
w.Write([]byte(registration))
case "POST":
// Get input
key := r.FormValue("key")
url := r.FormValue("url")
// Write to db
resp := make(chan bool)
save <- SaveRequest{key, url, resp}
_ = <-resp
w.Write([]byte("ok"))
}
return
}
// Redirect user based on the path.
resp := make(chan string)
code := r.URL.Path[1:]
lookup <- LookupRequest{code, resp}
url := <-resp
if url == "" {
http.Error(w, "Key not found", http.StatusNotFound)
return
}
http.Redirect(w, r, <-resp, http.StatusFound)
}
示例6: put
func put(w http.ResponseWriter, r *http.Request) {
keyName := r.FormValue("key")
value := r.FormValue("val")
c := appengine.NewContext(r)
key := datastore.NewKey("Entity", keyName, 0, nil)
entity := new(Entity)
entity.Value = value
result := map[string]string{
"error": "",
}
if _, err := datastore.Put(c, key, entity); err != nil {
result["error"] = fmt.Sprintf("%s", err)
}
// Set the value to speed up future reads - errors here aren't
// that bad, so don't worry about them
item := &memcache.Item{
Key: keyName,
Value: []byte(value),
}
memcache.Set(c, item)
bumpGeneration(c)
fmt.Fprintf(w, "%s", mapToJson(result))
}
示例7: serveFile
func serveFile(c *http.Conn, r *http.Request) {
path := r.Url.Path
// pick off special cases and hand the rest to the standard file server
switch ext := pathutil.Ext(path); {
case path == "/":
serveHtmlDoc(c, r, "doc/root.html")
case r.Url.Path == "/doc/root.html":
// hide landing page from its real name
http.NotFound(c, r)
case ext == ".html":
serveHtmlDoc(c, r, path)
case ext == ".go":
serveGoSource(c, path, &Styler{highlight: r.FormValue("h")})
default:
// TODO:
// - need to decide what to serve and what not to serve
// - don't want to download files, want to see them
fileServer.ServeHTTP(c, r)
}
}
示例8: articlesIndex
// articlesIndex is the handler for the site's index page.
// It shows up to the last 10 article summaries (all public info except
// for the Body) on a single page.
func articlesIndex(w http.ResponseWriter, r *http.Request) {
// Get the user-entered offset, or default to a zero offset.
offsetS := r.FormValue("Page")
var offset int
if len(offsetS) > 0 {
offset64, err := strconv.Btoi64(offsetS, 10)
check(err)
offset = int(offset64)
} else {
offset = 0
}
// Fetch public data from datastore, up to 10, with offset `offset * 10`
c := appengine.NewContext(r)
q := datastore.NewQuery("Article").Order("-Date").Filter("Public =", true).Limit(10).Offset(10 * offset)
a := make([]Article, 0, 10)
_, err := q.GetAll(c, &a)
check(err)
// Prepares the `indexTemplate.html` template
t := template.New("index")
t, err = t.ParseFile("templates/indexTemplate.html")
check(err)
// Executes the template, providing the data gathered from the datastore
err = t.Execute(w, a)
check(err)
}
示例9: RenderTiles
func RenderTiles(w http.ResponseWriter, req *http.Request) {
e := os.Remove("svg/test-surface.svg")
if e != nil {
os.Stderr.WriteString(e.String() + "\n")
}
if CurrentTiling == nil {
EmptySvg(w, req)
return
}
style := req.FormValue("style")
image := cairo.NewSurface("svg/test-surface.svg", 72*4, 72*4)
image.SetSourceRGB(0., 0., 0.)
image.SetLineWidth(.1)
image.Translate(72*2., 72*2.)
image.Scale(4., 4.)
if style == "edges" {
image.SetSourceRGBA(0., 0., 0., 1.)
CurrentTiling.DrawEdges(image)
} else if style == "plain" {
CurrentTiling.ColourFaces(image, zellij.OrangeBlueBrush)
} else {
CurrentTiling.ColourDebugFaces(image)
CurrentTiling.DrawDebugEdges(image)
}
image.Finish()
http.ServeFile(w, req, "svg/test-surface.svg")
}
示例10: GetOAuthParams
// Returns a map of all of the oauth_* (including signature) parameters for the
// given request, and the signature base string used to generate the signature.
func (s *HmacSha1Signer) GetOAuthParams(request *http.Request, clientConfig *ClientConfig, userConfig *UserConfig, nonce string, timestamp string) (map[string]string, string) {
request.ParseForm()
oauthParams := map[string]string{
"oauth_consumer_key": clientConfig.ConsumerKey,
"oauth_nonce": nonce,
"oauth_signature_method": "HMAC-SHA1",
"oauth_timestamp": timestamp,
"oauth_version": "1.0",
}
tokenKey, tokenSecret := userConfig.GetToken()
if tokenKey != "" {
oauthParams["oauth_token"] = tokenKey
}
signingParams := map[string]string{}
for key, value := range oauthParams {
signingParams[key] = value
}
for key, value := range request.URL.Query() {
//TODO: Support multiple parameters with the same name.
signingParams[key] = value[0]
}
for key, value := range request.Form {
//TODO: Support multiple parameters with the same name.
signingParams[key] = value[0]
}
signingUrl := fmt.Sprintf("%v://%v%v", request.URL.Scheme, request.URL.RawAuthority, request.URL.Path)
signatureParts := []string{
request.Method,
url.QueryEscape(signingUrl),
s.encodeParameters(signingParams)}
signatureBase := strings.Join(signatureParts, "&")
oauthParams["oauth_signature"] = s.GetSignature(clientConfig.ConsumerSecret, tokenSecret, signatureBase)
return oauthParams, signatureBase
}
示例11: uploadUser
func uploadUser(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
// No upload; show the upload form.
uploadUserTemplate.Execute(w, nil)
return
}
f, _, err := r.FormFile("image")
errors.Check(err)
defer f.Close()
// Grab the image data
i, _, err := image.Decode(f)
errors.Check(err)
var key string = keyOf()
// store image
i = picstore.Store(key, i, 320, "userimg")
i = picstore.Store(key, i, 180, "userthumb")
// generate result
w.Header().Set("Content-Type", "application/json")
w.Header().Set("cache-control", "no-cache")
w.Header().Set("Expires", "-1")
fmt.Fprintf(w, "{\"profilePicUrl\":\"uimg?id="+key+"\",\"profileThumbUrl\":\"utmb?id="+key+"\"}")
}
示例12: Delete
func Delete(url string) *HttpRequestBuilder {
var req http.Request
req.Method = "DELETE"
req.Header = http.Header{}
req.Header.Set("User-Agent", defaultUserAgent)
return &HttpRequestBuilder{url, &req, nil, map[string]string{}}
}
示例13: authCallback
func authCallback(w http.ResponseWriter, r *http.Request, oldSession *Session) {
// We cheat here -- font-end has direct access to the oauth data
code := r.FormValue("code")
context := appengine.NewContext(r)
var userKey datastore.Key
_, err := memcache.Gob.Get(context, "oauth-code-"+code, &userKey)
if err != nil {
w.Write([]byte("Invalid code"))
} else {
/*
auth := model.NewOAuth2(&userKey)
if _, err = datastore.Put(context, datastore.NewIncompleteKey(context, "Authentication", nil), auth); err != nil {
context.Errorf("Error saving: %v", err)
w.Write([]byte("Error saving: " + err.String()))
return
}
// replace session cookie
oldKey := datastore.NewKey(context, "Session", oldSession.Key, 0, nil)
datastore.Delete(context, oldKey)
session := NewSession(context)
oldSession.Key = session.Key
oldSession.Token = auth.Token
oldSession.SetCookie(w, context)
// redirect
*/
}
}
示例14: getSearchPanelHtml
func (h *SearchPanelHandler) getSearchPanelHtml(hr *http.Request) string {
fmt.Println("\n=== SearchPanelHandler : Requete reçue ====================")
fmt.Println(" URL : " + hr.RawURL)
hr.ParseForm()
askerId := GetFormValueAsInt(hr, "asker")
mdpr := GetFormValue(hr, "mdpr") // mot de passe restreint
tok := GetFormValue(hr, "tok")
compteOk := false
var compte *Compte
db, err := h.store.Connect()
if err != nil {
fmt.Printf("Erreur ouverture connexion BD dans makeBestiaryExtractHtml : %s\n", err.String())
return err.String()
}
defer db.Close()
if askerId > 0 && mdpr != "" {
compteOk, compte, err = h.store.CheckCompte(db, uint(askerId), mdpr)
}
if !compteOk {
return "Compte non authentifié"
}
if tok == "" {
return "Demande non comprise"
}
amis, err := h.store.GetPartageurs(db, askerId)
if err != nil {
return fmt.Sprintf("Erreur récupération amis : %s\n", err.String())
}
observations, err := h.store.SearchObservations(db, tok, askerId, amis)
if err != nil {
return fmt.Sprintf("Erreur recherche : %s\n", err.String())
}
if len(observations) == 0 {
return "Rien trouvé"
}
html := fmt.Sprintf("%d résultats :", len(observations))
html += "<table border='0' cellspacing='1' cellpadding='2' class='mh_tdborder' align='center'>"
html += "<tr class=mh_tdtitre><td class=mh_tdpage><b>Dist.</b></td><td class=mh_tdpage><b>Réf.</b></td><td class=mh_tdpage><b>Nom</b></td>"
html += "<td class=mh_tdpage><b>Position</b></td>"
html += "<td class=mh_tdpage><b>Vu par</b></td><td class=mh_tdpage><b>Le</b></td></tr>"
for _, o := range observations {
t := time.SecondsToLocalTime(o.Date)
var lien string
if o.Type == "troll" {
lien = fmt.Sprintf("<a href='javascript:EPV(%d)' class=mh_trolls_1 id=%d>%s</a>", o.Num, o.Num, o.Nom)
} else if o.Type == "monstre" {
lien = fmt.Sprintf("<a href='javascript:EMV(%d, 750, 550)' class=mh_monstres id=%d>%s</a>", o.Num, o.Num, o.Nom)
} else {
lien = o.Nom
}
dist := dist(compte.Troll.X, o.X, compte.Troll.Y, o.Y, compte.Troll.Z, o.Z)
btnVoir := fmt.Sprintf("<a x=%d y=%d z=%d class=gogo name=zoom>%d %d %d</a>", o.X, o.Y, o.Z, o.X, o.Y, o.Z)
html += fmt.Sprintf("<tr class=mh_tdpage><td>%d</td><td>%d</td><td>%s</td><td>%s</td><td>%d</td><td>%s</td></tr>", dist, o.Num, lien, btnVoir, o.Auteur, t.Format("02/01 à 15h04"))
}
html += "</table>"
return html
}
示例15: loginView
func loginView(req *http.Request, s *session) interface{} {
rurl := req.FormValue("back")
if rurl == "" {
rurl = "/"
}
if req.FormValue("username") != "" && req.FormValue("password") != "" {
user := study.GetUser(req.FormValue("username"))
if user != nil && user.CheckPass(req.FormValue("password")) {
s.User = user
s.Admin = false
if user.GetAttr("admin") == study.DBTRUE {
if _, ok := config.Conf.AdminUsers[user.Username]; ok {
s.Admin = true
} else {
user.SetAttr("admin", study.DBFALSE)
}
}
return redirectResponse(rurl)
}
return giveTplData{
"ReturnTo": rurl,
"Error": messages["BadUserOrPass"],
}
}
return giveTplData{
"ReturnTo": rurl,
}
}