本文整理匯總了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)
}
示例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)
}
示例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)
}
}
}
示例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)
}
}
示例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)
}
}
示例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)
}
示例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
}
示例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"])
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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)
}
示例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
}
示例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()
}
}