LoginSignup
1
2

More than 5 years have passed since last update.

gowebでresponseからdとsのキーを省きたいとき

Posted at

gowebでRESTFUl APIを使ってwebアプリを作ったんですが、
gowebでそのままだと、responseのJSONにdとsのキーがデフォルトで入るようになってて、ちょっとだけ不便でした。

例 : goweb レスポンス

{
  "d": { "mydata": true },
  "s": 200
}

みたいな感じで、そのままだとdにdataそのものが入り、sにstatusコードが入るようになってます。
これはこれで慣れれば使いやすいのかもしれませんが、生のJSONだけを取得したいときには不便。

省きたいときは以下のようにしましょう。

apiResponder := NewGowebAPIResponder(goweb.CodecService, goweb.Respond)
apiResponder.AlwaysEnvelopResponse = false
goweb.API = apiResponder

参考 https://github.com/stretchr/goweb/wiki/Controlling-API-output

1
2
0

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
1
2