golangでREST APIをやってみた。。。けどレスポンスにjson以外も欲しくない? IT by NHM - 2021-03-172021-03-17 goのjson rest APIの便利ツール。go-json-rest ant0ine/go-json-resthttps://github.com/ant0ine/go-json-rest jsonでいいじゃん。バイナリデータ欲しいならbase64エンコーディングして送れよって気になります。 しかし、単純にmp4や画像ファイル読み込む場合にいちいち手間をかけたくない。 json解析 base64デコード 画像表示 ちゃんと方法が記載されていました。これしておけばjson以外も返却可能。 API and static fileshttps://github.com/ant0ine/go-json-rest#api-and-static-files net/httpのhandleを利用できるとても良い例。パスのルーティングで切り分け可能 StripPrefixに設定したパス名は削除されるので、そのまま流したい場合は""にしておけば良い。 api.SetApp(router) http.Handle("/api/", http.StripPrefix("/api", api.MakeHandler())) http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir(".")))) log.Fatal(http.ListenAndServe(":8080", nil)) Share on Facebook Share Share on TwitterTweet Share on Pinterest Share Share on LinkedIn Share Share on Digg Share Tags: golang / json / rest