DRYな備忘録

Don't Repeat Yourself.

【Go言語】http.Request.Formとhttp.Request.PostFormの違い

golangnet/httpパッケージにあるRequestオブジェクトにはFormPostFormというフィールドがある。この違いはなんだ?

ドキュメント見る

http - The Go Programming Language

// Form contains the parsed form data, including both the URL
// field's query parameters and the POST or PUT form data.
// This field is only available after ParseForm is called.
// The HTTP client ignores Form and uses Body instead.
Form url.Values

// PostForm contains the parsed form data from POST or PUT
// body parameters.
// This field is only available after ParseForm is called.
// The HTTP client ignores PostForm and uses Body instead.
PostForm url.Values

どうやら

  • Formはquery parameterも取るのでURL中の?hoge=fugaも含めてurl.Valuesで返す
  • PostFormはpost bodyに含まれるものだけをurl.Valuesで返す

やってみる

$.ajax({url:"/?xxx=yyy",type:"POST",data:{aaa:true,bbb:{hoge:"fuga",ccc:{foo:12345}}}});
// fmt.Printf("%T %+v\n", req.Form, req.Form)
url.Values map[xxx:[yyy] aaa:[true] bbb[hoge]:[fuga] bbb[ccc][foo]:[12345]]
// fmt.Printf("%T %+v\n", req.PostForm, req.PostForm)
url.Values map[aaa:[true] bbb[hoge]:[fuga] bbb[ccc][foo]:[12345]]

雑感

  • はい

DRY