|
■No103240 (魔界の仮面弁士) に追記 > var tb = Request.TotalBytes; > string rawJson = (tb == 0) ? null : Encoding.UTF8.GetString(Request.BinaryRead(tb)); 訂正。
public string Post(string id, [FromBody] string value) { // // .NET Framework 4.8 + C# 11 using var ms = new MemoryStream(); if (Request.InputStream.CanSeek) { Request.InputStream.Position = 0; } Request.InputStream.CopyTo(ms); string rawJson = Encoding.UTF8.GetString(ms.ToArray());
// 以下略 }
> MVC コントローラーは、Content-Type によって振る舞いを変える
curl -X POST -H "Content-Type:application/json;charset=UTF-8" --data-raw "{\"id\":\"jsonId\",\"value\":\"jsonValue\"}" --include "http://localhost:43138/Test/Post" ⇒ id=="jsonId", value=="jsonValue" ⇒ Request.InputStream → 「{"id":"jsonId","value":"jsonValue"}」
curl -X POST -H "Content-Type:application/x-www-form-urlencoded" --data-raw "{\"id\":\"jsonId\",\"value\":\"jsonValue\"}" --include "http://localhost:43138/Test/Post" ⇒ id==null, value==null ⇒ Request.InputStream → 「{"id":"jsonId","value":"jsonValue"}」
curl -X POST -H "Content-Type:text/plain;charset=UTF-8" --data-raw "{\"id\":\"jsonId\",\"value\":\"jsonValue\"}" --include "http://localhost:43138/Test/Post" ⇒ id==null, value==null ⇒ Request.InputStream → 「{"id":"jsonId","value":"jsonValue"}」
curl -X POST -H "Content-Type:application/xml" --data-raw "<x><id>xmlId</id><value>xmlValue</value></x>" --include "http://localhost:43138/Test/Post" ⇒ id==null, value==null ⇒ Request.InputStream → 「<x><id>xmlId</id><value>xmlValue</value></x>」
|