LoginSignup
1
0

More than 1 year has passed since last update.

GoのPostでBindJSONで値が取得できない

Posted at

はじめに

この記事は2022年8月にまとめていた「細かいつまずいたことをメモしておく(8月編)をそれぞれ投稿した内容になります
解決方法が最新でない可能性もありますのでご了承ください

問題

以下の記事を参考にPOSTを作成していたがPostmanで送った値が取得できなかった

解決方法

ここで私が解決に至った手順を載せる

1. 構造体にJsonで送る際の名前を設定する

model.go
type Item struct {
    Title       string `json:"title"`
    Description string `json:"description"`
}

josn:"title"のようにつけないとPostmanのデータでtitleというKeyで送ることができない(Tが大文字で構造体には設定されているため)

2. PostmanではJSONで送信する必要がある

これがかなり沼ってしまった
最初PostmanでParamsやBody(form-data)でデータを送っていたがBindJSONをするならBody(raw)で送る必要があった

{
  "title": "test",
  "description": "test2"
}

そして落とし穴はテキストをJSONに変更することだった
ここを変更しないとContent-Type:application/jsonで送られないので値が読み取れない

image.png

参考

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