"Unsupported Media Type",在react-native中请求post时出现415错误

"Unsupported Media Type", 415 eror when post request in react-native

我正在尝试在 react-native 应用程序中获取我的 asp .net 核心 api。当我与邮递员一起测试时,这个 api 工作正常。但是在本机反应中我有这个错误。

["https://tools.ietf.org/html/rfc7231#section-6.5.13", "Unsupported Media Type", 415, "00-2608a636e0aeaf4bb2b29a2e1b54370a-2145e9063626d940-00"]

这是我的api。我试过 public JsonResult 但没有任何变化。

 [HttpPost("register")]
        public IActionResult Register([FromBody] TbPersonel model)
        {
            try
            {
                // SOME CASES

                if (dt.Rows.Count > 0)
                {
                    model.login = true;                   
                    return new JsonResult(model.login);
                }
                else
                {
                    model.login = false;                   
                    return new JsonResult(model.login);
                }
            }
            catch (Exception e)
            {
                Logla.Log(MethodBase.GetCurrentMethod().Name, ControllerContext.ActionDescriptor.ControllerName, e);
                return new JsonResult(e);
            }
        }

React-Native

 fetch('http:/.../api/login/register/',{
            method:'post',
            header:{
                'Accept': 'application/json',
                // 'Content-type': 'application/json'
        'Content-Type': 'application/json; charset=utf-8'
            },
            body:JSON.stringify({               
                'kullaniciAdi': userName,
                'sifre': userPassword
            })          
        })    
        .then((response) => response.json())
         .then((responseJson)=>{
       console.log(Object.values(responseJson));

Postman Post request

Postman headers

问题出在fetch的参数上。 fetch 没有任何名为 header 的参数,但它是带有 s 的 headers。因此,您的代码没有要求正确的 Content-type 和接受 Headers。要解决此问题,只需将 s 添加到 header 键即可:

fetch('http:/.../api/login/register/',{
        method:'post',
        headers:{
            'Accept': 'application/json',
            // 'Content-type': 'application/json'
    'Content-Type': 'application/json; charset=utf-8'
        },
        body:JSON.stringify({               
            'kullaniciAdi': userName,
            'sifre': userPassword
        })          
    })    
    .then((response) => response.json())
     .then((responseJson)=>{
   console.log(Object.values(responseJson));

不幸的是,Javascript 没有突出显示此错误。