0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GAE/goで外部APIを使った際にハマったところ

Last updated at Posted at 2018-08-28

はじめに

Goで書いたbotをGAEで運用しようとした際、通信部分(外部APIへのリクエスト)で地味にハマったため簡単にまとめていこうと思います。

実行環境

ローカル:Go v1.10.1
GAE:Go v1.10

Google App Engine(GAE)とは

Google App Engine(GAE)とは?

Google App Engine は、Google のインフラの上でアプリケーションを作り、実行できるようにする PaaS ( Platform as a Service : サービスとしてのプラットフォーム)です。App Engine アプリケーションは、作るのもメンテナンスするのも簡単で、トラフィックやデータストレージのニーズの増減にあわせてスケーリングするのも簡単です。App Engine を使えば、自分でメンテナンスしなければならないサーバーはなくなります。単純にアプリケーションをアップロードすれば、それだけで実行できます。

Googleが提供しているクラウドサーバーです。

外部APIを扱う

通常Getリクエストを行う場合はhttp.get(url)を行えばよいが、GAEで行う場合はContextを使うことを配慮しなければなりません。

ローカルで実行する場合

main.go
func main() {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
	response, err := http.Get(url)
	if err != nil {
		log.Println(err)
	}
	if response != nil {
		defer response.Body.Close()
	}

	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Println(err)
	}

	fmt.Println(body)
}

GAEの場合

main.go
func main() {
	http.HandleFunc("/", handler)
	appengine.Main()
}

func handler(w http.ResponseWriter, r *http.Request) {
	context := appengine.NewContext(r)
	httpClient := urlfetch.Client(context)

	response, err := httpClient.Get(url)
	if err != nil {
		log.Println(err)
	}
	if response != nil {
		defer response.Body.Close()
	}

	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		log.Println(err)
	}

	fmt.Println(body)
}

ハマったポイント

ローカルで実行する場合はhttp.Get(url)でよかったが、GAEで行う場合はappengine.NewContext(*http.Request)でcontextを作成し、urlfetch.Client(context)のようにurlfetchを利用してリクエストを行わなければならなかった。

まとめ

GAEの場合は通常と異なり、GAEのContextを作成し、urlfetchを用いてリクエストを行うことが必要になります。

.go
func handler(w http.ResponseWriter, r *http.Request) {
    context := appengine.NewContext(r)
    httpClient := urlfetch.Client(context)

	response, err := httpClient.Get(url)
	if err != nil {
		log.Println(err)
	}
	if response != nil {
		defer response.Body.Close()
	}
}

参考

The urlfetch package
goでのhttpの書き方あれこれ

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?