[HttpPost] [Route(“/getter/validatecookie”)] public async Task GetRankings([FromBody] string cookie) { int world = 5; ApiGetter getter = new ApiGetter(_config, cookie); if (!await IsValidCookie(getter, world)) { return BadRequest(“Invalid CotG Session”); } HttpContext.Session.SetString(“cotgCookie”, cookie); return Ok(); }
我的请求:
$http.post(ENDPOINTS["Validate Cookie"], cookie , {'Content-Type': 'application/json'});
cookie我从用户输入中发送的字符串在哪里。
cookie
该请求将使用适当的数据发布到端点。但是,我的字符串始终为null。我尝试删除[FromBody]标签,以及=在运气好的情况下在发布的数据前添加。我还尝试了使用上述所有组合添加和删除不同的内容类型。
[FromBody]
=
我执行此特定操作的原因很长,与这个问题无关。
无论我做什么,为什么我的参数始终为null?
编辑:我也尝试使用 {cookie: cookie}
{cookie: cookie}
Edit2 :请求:
Request URL:http://localhost:54093/getter/validatecookie Request Method:POST Status Code:400 Bad Request Remote Address:[::1]:54093
响应标题
Content-Type:text/plain; charset=utf-8 Date:Mon, 23 Jan 2017 03:12:54 GMT Server:Kestrel Transfer-Encoding:chunked X-Powered-By:ASP.NET X-SourceFiles:=?UTF-8?B?QzpcVXNlcnNcRG91Z2xhc2cxNGJcRG9jdW1lbnRzXFByb2dyYW1taW5nXENvdEdcQ290RyBBcHBcc3JjXENvdEdcZ2V0dGVyXHZhbGlkYXRlY29va2ll?=
请求标题
POST /getter/validatecookie HTTP/1.1 Host: localhost:54093 Connection: keep-alive Content-Length: 221 Accept: application/json, text/plain, */* Origin: http://localhost:54093 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36 Content-Type: application/json;charset=UTF-8 Referer: http://localhost:54093/ Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.8
要求有效载荷
=sec_session_id=[redacted]; _ga=[redacted]; AWSELB=[redacted]
问题是is Content- Type是application/json,而请求有效载荷实际上是text/plain。这将导致415不支持的媒体类型HTTP错误。
Content- Type
application/json
text/plain
您至少有两个选项可以使Content-Type内容与实际内容对齐。
Content-Type
保留Content-Typeas application/json并确保请求有效负载是有效的JSON。例如,使您的请求有效负载为:
{ "cookie": "=sec_session_id=[redacted]; _ga=[redacted]; AWSELB=[redacted]" }
然后,动作签名需要接受与JSON对象具有相同形状的对象。
public class CookieWrapper { public string Cookie { get; set; } }
代替CookieWrapper类,或者您可以接受动态或a Dictionary<string, string>并像cookie["cookie"]在端点中那样对其进行访问
CookieWrapper
Dictionary<string, string>
cookie["cookie"]
public IActionResult GetRankings([FromBody] CookieWrapper cookie) public IActionResult GetRankings([FromBody] dynamic cookie) public IActionResult GetRankings([FromBody] Dictionary<string, string> cookie)
另一种选择是将项目更改Content-Type为text/plain,并向项目中添加纯文本输入格式器。为此,请创建以下类。
public class TextPlainInputFormatter : TextInputFormatter { public TextPlainInputFormatter() { SupportedMediaTypes.Add("text/plain"); SupportedEncodings.Add(UTF8EncodingWithoutBOM); SupportedEncodings.Add(UTF16EncodingLittleEndian); } protected override bool CanReadType(Type type) { return type == typeof(string); } public override async Task<InputFormatterResult> ReadRequestBodyAsync( InputFormatterContext context, Encoding encoding) { string data = null; using (var streamReader = context.ReaderFactory( context.HttpContext.Request.Body, encoding)) { data = await streamReader.ReadToEndAsync(); } return InputFormatterResult.Success(data); } }
并配置Mvc以使用它。
services.AddMvc(options => { options.InputFormatters.Add(new TextPlainInputFormatter()); });
https://github.com/aspnet/Mvc/issues/5137