以前、HTMLフォームでPOSTされたデータに対してのModelBindingを試しました。
ただ、HTMLフォームではなくJSONをPOSTした場合のModelBindingはどうなるのだろうと考え、公式ドキュメントを読んでみたところ、 同一のソースコードでJSONも処理できそうな記載がありました。
そこで、実際に試してみることにしました。
環境
- Nancy 1.1
- DHC - REST/HTTP API Client 0.7.20
流れ
DHCでJSONデータの送信
以下の様なデータを作り、送信します。

実際にPOSTされる内容は以下の通りです。
POST /vtom HTTP/1.1
Host: localhost:8765
Content-Type: application/json
Content-Length: 187
{
text: "hoge",
textArea: "fuga",
tel: "987-6543-210",
url: "http://example.com",
email: "piyo@example.com",
date: "2015/01/02",
checkbox: true,
radioButtonValue: "good"
}
レスポンスの表示
上記のソースコードにある通り、ModelBindingが行われ、その結果のJSONが返ってきました。
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Mon, 20 Apr 2015 12:36:55 GMT
{"text":"hoge","textArea":"fuga","tel":"987-6543-210","url":"http://example.com","email":"piyo@example.com","date":"2015-01-02T00:00:00.0000000+09:00","checkbox":true,"radioButtonValue":"good","isGood":true,"isNormal":false,"isBad":false}
HTMLフォームやJSONの違いを意識することなく、ModelBindingできそうです。