亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

顯示 URL 而非 Go 請求 GCP 響應的代碼

顯示 URL 而非 Go 請求 GCP 響應的代碼

Go
慕神8447489 2022-06-13 15:20:06
我很難在 Google Cloud Platform 中顯示方向請求的結果。這是我嘗試過的:package mainimport (    "fmt"    "net/http"    "context"    "io/ioutil"    "google.golang.org/appengine"    "google.golang.org/appengine/urlfetch")const directionAPIKey = "MyAppKey"const directionURL = "https://maps.googleapis.com/maps/api/directions/json?origin=%s&destination=%s&mode=%s&key=%s"func main() {    http.HandleFunc("/", handler)}func handler(w http.ResponseWriter, r *http.Request) {    ctx := appengine.NewContext(r)    direction, err := fetchDirection(ctx, r.FormValue("origin"), r.FormValue("destination"), r.FormValue("mode"))    if err != nil {        http.Error(w, err.Error(), http.StatusInternalServerError)        return    }    w.Header().Add("Content-Type", "application/json; charset=utf-8")    w.Write(direction)}func fetchDirection(ctx context.Context, origin string, destination string, mode string) ([]byte, error) {    client := urlfetch.Client(ctx)    resp, err := client.Get(fmt.Sprintf(directionURL, origin, destination, mode, directionAPIKey))    if err != nil {        return nil, err    }    defer resp.Body.Close()    return ioutil.ReadAll(resp.Body)}我成功部署了我的應用程序,但是,上面代碼的結果是:Get https://maps.googleapis.com/maps/api/directions/json?origin=&destination=&mode=&key=APIKey not an App Engine context而不是請求的結果。這是來自瀏覽器的圖像。我怎樣才能得到想要的結果,而不僅僅是那個 URL?提前致謝。
查看完整描述

1 回答

?
四季花海

TA貢獻1811條經驗 獲得超5個贊

我發現當您的 AppEngine 應用程序將運行時設置為 go111 并且應用程序導入任何“google.golang.org/appengine”包但應用程序不調用“ appengine.Main”


method func main() {

    http.HandleFunc("/", handler)

    appengine.Main()

}


查看完整回答
反對 回復 2022-06-13
  • 1 回答
  • 0 關注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號