Google App Engine Go HTTP Post []byte

2019-05-23 08:29发布

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条回答
Fickle 薄情
2楼-- · 2019-05-23 08:51
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)
    }
}
查看更多
登录 后发表回答