Google App Engine Go HTTP Post []byte

huangapple go评论83阅读模式
英文:

Google App Engine Go HTTP Post []byte

问题

我想通过Go语言在Google App Engine上使用HTTP的POST方法发送[]byte数据。我应该如何构建请求的bodyType和body

英文:

I want to send []byte data over http via post on Google App Engine in Go. How do I construct bodyType and, body for the request?

答案1

得分: 3

package app

import (
"http"
"appengine"
"appengine/urlfetch"
"bytes"
"fmt"
)

func init() {
http.HandleFunc("/post", post)
}

func post(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
bs := []byte{1,2,3}
buf := bytes.NewBuffer(bs)
client := http.Client{Transport: &urlfetch.Transport{Context:c}}
if _, err := client.Post("http://localhost:8080/", "application/octet-stream", buf); err != nil {
fmt.Println(err)
}
}

英文:
package app

import (
    "http"
    "appengine"
    "appengine/urlfetch"
    "bytes"
    "fmt"
)

func init() {
    http.HandleFunc("/post", post)
}

func post(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    bs := []byte{1,2,3}
    buf := bytes.NewBuffer(bs)
    client := http.Client{Transport: &urlfetch.Transport{Context:c}}
    if _, err := client.Post("http://localhost:8080/", "application/octet-stream", buf); err != nil {
	    fmt.Println(err)
    }
}

huangapple
  • 本文由 发表于 2011年11月3日 01:47:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/7984931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定