英文:
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论