I'm trying to fetch my asp .net core api in react-native app. When i am testing wiht postman this api is working fine. But in react-native i have this eror.
["https://tools.ietf.org/html/rfc7231#section-6.5.13", "Unsupported Media Type", 415, "00-2608a636e0aeaf4bb2b29a2e1b54370a-2145e9063626d940-00"]
This is my api. I have tried public JsonResult but nothing change.
[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));
CodePudding user response:
The problem is in the parameters of fetch. fetch doesn't have any parameter named header
but it's headers
with an s. So, your code wasn't asking for the correct Content-type and Accept Headers. To fix this just add an s to the header key :
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));
Unfortunately, Javascript doesn't highlight this error.