小编典典

Google App Engine Go HTTP发布[] byte

go

我想通过Go上Google App Engine上的帖子通过http发送[]
byte数据。如何为请求构造bodyType和body


阅读 250

收藏
2020-07-02

共1个答案

小编典典

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)
    }
}
2020-07-02