Keep Getting error 415 with get fetch

osyris 236 Reputation points
2021-09-02T17:55:59.487+00:00

I am simply trying to get the amount number of objects with the same key form the database
No matter what i try i keep getting error 415 While the parameters are both strings

C#

 public class TokenDto
 {
 public string Acces { get; set; }
 }


 [HttpGet("CardCount")]
 public async Task<IActionResult> CardCount(TokenDto dto)
 {
 if (ModelState.IsValid)
 {
 if (dto.Acces == null)
 return BadRequest("Could not find acces token");

 var count = _dbContext.Cards.Where(x => x.AccesToken == dto.Acces).Count();

 return Ok(count);
 }
 return BadRequest("no accesToken found");
 }

client side Reactjs:

try{
     await axios.get("https://localhost:44332/CardCount", {"acces" :  cookie.Default} )
    }catch(e){
      console.log("error ", e)
    }

error output:

error Error: Request failed with status code 415
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:62)

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,140 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,246 questions
{count} votes

3 answers

Sort by: Most helpful
  1. osyris 236 Reputation points
    2021-09-02T22:14:40.65+00:00

    So i have found out how to solve it but i dont know why it works that way

    when i turn it to a Post method all of a sudden i dont get a error 415 anymore
    Can someone explain why that is and how i can make this work as a Get method since its faster.

    Also this part is not working well:
    var count = _dbContext.Cards.Where(x => x.AccesToken == dto.Acces).Count();

    it keeps returning 0 while there were 4 items in the "Card" database table with the same Accestoken and the same as the dto.Acces
    i have checked it in my database and checked if both Tokens were corrent.

    0 comments No comments

  2. AgaveJoe 26,186 Reputation points
    2021-09-03T16:01:12.357+00:00

    when i turn it to a Post method all of a sudden i dont get a error 415 anymore Can someone explain why that is and how i can make this work as a Get method since its faster.

    The 415 is an unsupported media type. It means the data format you sent to the action was unexpected. I suspect the TokenDto class does not match the data but you did not share the model as requested. I'm not sure why you think a GET is faster than a POST which is not true. Perhaps you mean GET is easier to test with the browser???

    Anyway, a POST is the standard for submitting an object. To use a GET, add the data to the URL. I recommend learning GET and POST basics.

    "https://localhost:44332/CardCount?acces=" + somevalue  
    

    Also this part is not working well:
    var count = _dbContext.Cards.Where(x => x.AccesToken == dto.Acces).Count();

    it keeps returning 0 while there were 4 items in the "Card" database table with the same Accestoken and the same as the dto.Acces
    i have checked it in my database and checked if both Tokens were corrent.

    We cannot see your Cards table or the value of dto.Acces. My best guess is dto.Acces does not exist in the table or the table is empty. I recommend running your code though the debugger. You can also use the browser's dev tools to see what values are being submitted to the server.

    0 comments No comments

  3. Bruce (SqlWork.com) 55,041 Reputation points
    2021-10-04T14:45:26.953+00:00

    You can not pass json objects with a get. Change action to

    [HttpGet("CardCount")]
     public async Task<IActionResult> CardCount(string acces)
    
    0 comments No comments