當前位置: 首頁>>代碼示例>>Golang>>正文


Golang http.Post函數代碼示例

本文整理匯總了Golang中http.Post函數的典型用法代碼示例。如果您正苦於以下問題:Golang Post函數的具體用法?Golang Post怎麽用?Golang Post使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Post函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: handleDeleteForm

func handleDeleteForm(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	_, kind := path.Split(r.URL.Path)
	key := DefaultMaster.delegator.(DelegatorBroadcaster).KeyFromForm(r.Form)
	ser := DefaultMaster.delegator.Delegate(kind)
	keys := ser.Keys()
	n := ser.Init()
	for _, v := range keys {
		if v != key {
			n = n.Insert(ser.At(v))
		}
	}
	ser.All(n)
	s := ser.At(key).(SerialSender)
	DefaultMaster.Delete(s)
	out := bytes.NewBufferString("")
	host := s.Host()
	if host != "" {
		DefaultMaster.save(out, s)
		http.Post("http://"+host+DefaultMaster.deletePattern+kind, "application/octet-stream", out)
	} else {
		bc := DefaultMaster.delegator.(DelegatorBroadcaster)
		for _, h := range bc.Hosts() {
			out = bytes.NewBufferString("")
			DefaultMaster.save(out, s)
			http.Post("http://"+h+DefaultMaster.deletePattern+kind, "application/octet-stream", out)
		}
	}
	DefaultMaster.Logger.Printf("%v erfolgreich gelöscht", s.Log())
	redir := "http://" + r.Host + r.FormValue("Redir")
	w.SetHeader("Location", redir)
	w.WriteHeader(302)
}
開發者ID:shaban,項目名稱:Kengal-Server,代碼行數:33,代碼來源:gobzip.go

示例2: handleReplaceForm

func handleReplaceForm(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	_, kind := path.Split(r.URL.Path)
	ser := DefaultMaster.delegator.Delegate(kind).(SerializerFormParser)
	s := ser.NewFromForm(r.Form)
	if s == nil {
		r.Form = nil
		w.SetHeader("Location", r.Referer)
		w.WriteHeader(302)
		return
	}
	ser.Replace(s)
	DefaultMaster.Save(s)
	out := bytes.NewBufferString("")
	host := s.Host()
	if host != "" {
		DefaultMaster.save(out, s)
		http.Post("http://"+host+DefaultMaster.replacePattern+kind, "application/octet-stream", out)
	} else {
		bc := DefaultMaster.delegator.(DelegatorBroadcaster)
		for _, h := range bc.Hosts() {
			out = bytes.NewBufferString("")
			DefaultMaster.save(out, s)
			http.Post("http://"+h+DefaultMaster.replacePattern+kind, "application/octet-stream", out)
		}
	}
	DefaultMaster.Logger.Printf("%v erfolgreich modifiziert", s.Log())
	redir := "http://" + r.Host + r.FormValue("Redir")
	w.SetHeader("Location", redir)
	w.WriteHeader(302)
}
開發者ID:shaban,項目名稱:Kengal-Server,代碼行數:31,代碼來源:gobzip.go

示例3: main

func main() {
	flag.Parse()

	listener, err := net.Listen("tcp", *listenAddr)
	if err != nil {
		panic(err)
	}

	conn, err := listener.Accept()
	if err != nil {
		panic(err)
	}

	buf := new(bytes.Buffer)

	// initiate new session and read key
	log.Stderr("Attempting connect", *destAddr)
	buf.Write([]byte(*destAddr))
	resp, err := http.Post(
		"http://"+*httpAddr+"/create",
		"text/plain",
		buf)
	if err != nil {
		panic(err)
	}
	key, _ := ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	log.Stderr("Connected, key", key)

	// ticker to set a rate at which to hit the server
	tick := time.NewTicker(int64(*tickInterval) * 1e6)
	read := makeReadChan(conn, bufSize)
	buf.Reset()
	for {
		select {
		case <-tick.C:
			// write buf to new http request
			req := bytes.NewBuffer(key)
			buf.WriteTo(req)
			resp, err := http.Post(
				"http://"+*httpAddr+"/ping",
				"application/octet-stream",
				req)
			if err != nil {
				log.Stderr(err.String())
				continue
			}
			// write http response response to conn
			io.Copy(conn, resp.Body)
			resp.Body.Close()
		case b := <-read:
			buf.Write(b)
		}
	}
}
開發者ID:welterde,項目名稱:gohttptun,代碼行數:56,代碼來源:client.go

示例4: pop

func pop() {
	_, err := http.Post("http://"+*addr+"/done", "text/plain", strings.NewReader("done"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "http.Post: %v", err)
		os.Exit(1)
	}
}
開發者ID:alloy-d,項目名稱:now.go,代碼行數:7,代碼來源:now.go

示例5: main

func main() {
	flag.Parse()
	if *addr {
		l, err := net.Listen("tcp", "127.0.0.1:0")
		if err != nil {
			log.Fatal(err)
		}
		defer l.Close()
		fmt.Print(l.Addr())
		return
	}
	url := flag.Arg(0)
	if url == "" {
		log.Fatal("no url supplied")
	}
	var r *http.Response
	var err os.Error
	if *post != "" {
		b := strings.NewReader(*post)
		r, err = http.Post(url, "application/x-www-form-urlencoded", b)
	} else {
		r, err = http.Get(url)
	}
	if err != nil {
		log.Fatal(err)
	}
	defer r.Body.Close()
	_, err = io.Copy(os.Stdout, r.Body)
	if err != nil {
		log.Fatal(err)
	}
}
開發者ID:go-nosql,項目名稱:golang,代碼行數:32,代碼來源:get.go

示例6: tmp

func tmp() {
	data, err := json.Marshal(map[string]interface{}{
		"method": "getinfo",
		"id":     1,
		"params": []interface{}{},
	})
	if err != nil {
		log.Fatalf("Marshal: %v", err)
	}
	resp, err := http.Post("http://user:[email protected]:8332",
		"application/json", strings.NewReader(string(data)))
	if err != nil {
		log.Fatalf("Post: %v", err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalf("ReadAll: %v", err)
	}
	result := make(map[string]interface{})
	err = json.Unmarshal(body, &result)
	if err != nil {
		log.Fatalf("Unmarshal: %v", err)
	}
	log.Println(result)
}
開發者ID:ThePiachu,項目名稱:Go-HTTP-JSON-RPC,代碼行數:26,代碼來源:test.go

示例7: StatusesUpdate

func (c *Client) StatusesUpdate(status string, replyId uint64) (err os.Error) {
	var params string

	if status == "" {
		return os.NewError("must need \"status\" parameter.")
	}

	params = addParam(params, "status", http.URLEscape(status))

	if replyId != 0 {
		params = addParam(params, "in_reply_to_status_id", fmt.Sprintf("%d", replyId))
	}

	url := c.makeAuthURL(statusesUpdate, params)
	res, err := http.Post(url, "", bytes.NewBufferString(""))
	if err != nil {
		return err
	}

	if res.Status != "200 OK" {
		return os.NewError("Request failed")
	}

	return nil
}
開發者ID:kazura-utb,項目名稱:TwitterWithGo,代碼行數:25,代碼來源:twitter.go

示例8: Verify

func Verify(r *http.Request) string {

	if err := r.ParseForm(); err != nil {
		return "Anonymous"
	}

	token := r.FormValue("assertion")
	url := "https://browserid.org/verify"
	bodytype := "application/x-www-form-urlencoded"
	body := strings.NewReader("assertion=" + token + "&audience=" + r.Host)

	var response_body []byte
	res, err := http.Post(url, bodytype, body)
	if err != nil {
		fmt.Println("err=", err)
		return "Anonymous"
	} else {
		response_body, _ = ioutil.ReadAll(res.Body)
		res.Body.Close()
	}

	var f interface{}
	json.Unmarshal(response_body, &f)

	m := f.(map[string]interface{})
	return fmt.Sprintf("%s", m["email"])
}
開發者ID:elathan,項目名稱:gobrowserid,代碼行數:27,代碼來源:browserid.go

示例9: Call

func Call(address string, method string, id interface{}, params []interface{}) (map[string]interface{}, os.Error) {
	data, err := json.Marshal(map[string]interface{}{
		"method": method,
		"id":     id,
		"params": params,
	})
	if err != nil {
		log.Fatalf("Marshal: %v", err)
		return nil, err
	}
	resp, err := http.Post(address,
		"application/json", strings.NewReader(string(data)))
	if err != nil {
		log.Fatalf("Post: %v", err)
		return nil, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalf("ReadAll: %v", err)
		return nil, err
	}
	result := make(map[string]interface{})
	err = json.Unmarshal(body, &result)
	if err != nil {
		log.Fatalf("Unmarshal: %v", err)
		return nil, err
	}
	//log.Println(result)
	return result, nil
}
開發者ID:ThePiachu,項目名稱:Go-HTTP-JSON-RPC,代碼行數:31,代碼來源:httpjsonrpcClient.go

示例10: ShortenURL

func ShortenURL(longUrl string) (shortenUrl string, err os.Error) {
	var encbuf bytes.Buffer
	enc := json.NewEncoder(&encbuf)
	err = enc.Encode(map[string]string{"longUrl": longUrl})
	if err != nil {
		return
	}
	res, err := http.Post("https://www.googleapis.com/urlshortener/v1/url", "application/json", strings.NewReader(encbuf.String()))
	if err != nil {
		return
	}
	if res.StatusCode != 200 {
		err = os.NewError("failed to post")
		return
	}
	b, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return
	}
	var decbuf bytes.Buffer
	decbuf.Write(b)
	dec := json.NewDecoder(&decbuf)
	var out map[string]interface{}
	err = dec.Decode(&out)
	if err != nil {
		return
	}
	shortenUrl = out["id"].(string)
	return
}
開發者ID:mattn,項目名稱:go-urlshortener,代碼行數:30,代碼來源:urlshortener.go

示例11: FriendshipsCreate

func (c *Client) FriendshipsCreate(userId uint64, screenName string, follow bool) (err os.Error) {
	var params string

	if userId != 0 {
		params = addParam(params, "user_id", fmt.Sprintf("%d", userId))
	}
	if screenName != "" {
		params = addParam(params, "screen_name", screenName)
	}
	if follow == true {
		params = addParam(params, "follow", "true")
	}

	url := c.makeAuthURL(friendshipsCreate, params)
	res, err := http.Post(url, "", bytes.NewBufferString(""))
	if err != nil {
		return err
	}

	if res.Status != "200 OK" {
		return os.NewError("Request failed.")
	}

	return nil
}
開發者ID:kazura-utb,項目名稱:TwitterWithGo,代碼行數:25,代碼來源:twitter.go

示例12: request

func (c *Client) request(client *http.Client, credentials *Credentials, urlStr string, param web.Values) (*Credentials, web.Values, os.Error) {
	c.SignParam(credentials, "POST", urlStr, param)
	resp, err := http.Post(urlStr, "application/x-www-form-urlencoded", bytes.NewBuffer(param.FormEncodedBytes()))
	if err != nil {
		return nil, nil, err
	}
	p, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	if err != nil {
		return nil, nil, err
	}
	if resp.StatusCode != 200 {
		return nil, nil, os.NewError(fmt.Sprintf("OAuth server status %d, %s", resp.StatusCode, string(p)))
	}
	m := make(web.Values)
	err = m.ParseFormEncodedBytes(p)
	if err != nil {
		return nil, nil, err
	}
	credentials = &Credentials{Token: m.Get("oauth_token"), Secret: m.Get("oauth_token_secret")}
	if credentials.Token == "" {
		return nil, nil, os.NewError("No OAuth token in server result")
	}
	if credentials.Secret == "" {
		return nil, nil, os.NewError("No OAuth secret in server result")
	}
	return credentials, m, nil
}
開發者ID:adamsxu,項目名稱:twister,代碼行數:28,代碼來源:oauth.go

示例13: handleInsertForm

func handleInsertForm(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	_, kind := path.Split(r.URL.Path)
	ser := DefaultMaster.delegator.Delegate(kind).(SerializerFormParser)
	s := ser.NewFromForm(r.Form)
	if s == nil {
		r.Form = nil
		w.SetHeader("Location", r.Referer)
		w.WriteHeader(302)
		return
	}
	ser.All(ser.Insert(s))
	err := DefaultMaster.Save(s)
	if err != nil {
		DefaultMaster.Logger.Printf("Problem: Einfügen von %s auf Hauptserver fehlgeschlagen", s.Log())
		w.WriteHeader(500)
	}
	out := bytes.NewBufferString("")
	out = bytes.NewBufferString("")
	DefaultMaster.save(out, s)

	host := s.Host()
	switch host {
	case "":
		bc := DefaultMaster.delegator.(DelegatorBroadcaster)
		for _, h := range bc.Hosts() {
			resp, err := http.Post("http://"+h+DefaultMaster.insertPattern+kind, "application/octet-stream", out)
			if resp.StatusCode == 500 || err != nil {
				DefaultMaster.Logger.Printf("Problem: Einfügen von %s auf Server %s fehlgeschlagen", s.Log(), h)
			}
		}
	case "NO":
		break
	default:
		resp, err := http.Post("http://"+host+DefaultMaster.insertPattern+kind, "application/octet-stream", out)
		if resp.StatusCode == 500 || err != nil {
			DefaultMaster.Logger.Printf("Problem: Einfügen von %s auf Server %s fehlgeschlagen", s.Log(), host)
		}
	}
	DefaultMaster.Logger.Printf("%v erfolgreich angelegt", s.Log())
	redir := "http://" + r.Host + r.FormValue("Redir") + strconv.Itoa(s.Key())
	w.SetHeader("Location", redir)
	w.WriteHeader(302)
}
開發者ID:shaban,項目名稱:Kengal-Server,代碼行數:44,代碼來源:gobzip.go

示例14: SendChanClient

func SendChanClient(host, id string) (send chan<- string) {
	sendc := make(chan string)
	send = sendc
	url := fmt.Sprintf("http://%s/%s/Send", host, id)
	go func() {
		for message := range sendc {
			http.Post(url, "text/plain", bytes.NewBufferString("msg="+message))
		}
	}()
	return
}
開發者ID:skelterjohn,項目名稱:sao,代碼行數:11,代碼來源:comm.go

示例15: maybeReportToDashboard

// maybeReportToDashboard reports path to dashboard unless
// -dashboard=false is on command line.  It ignores errors.
func maybeReportToDashboard(path string) {
	// if -dashboard=false was on command line, do nothing
	if !*reportToDashboard {
		return
	}

	// otherwise lob url to dashboard
	r, _ := http.Post(dashboardURL, "application/x-www-form-urlencoded", strings.NewReader("path="+path))
	if r != nil && r.Body != nil {
		r.Body.Close()
	}
}
開發者ID:vablaya,項目名稱:gopp-cs345,代碼行數:14,代碼來源:download.go


注:本文中的http.Post函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。