question

osyris-3187 avatar image
0 Votes"
osyris-3187 asked Bruce-SqlWork answered

Keep Getting error 415 with get fetch

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)

dotnet-aspnet-core-generaldotnet-aspnet-generaldotnet-aspnet-core-webapi
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The 415 status is unsupported media type most likely due to the unknown input TokenDto parameter.

Usually tokens are passed in the authorization header as a bearer tokens not openly in the URL and authentication cookies are passed in the cookie header. Can you explain the general design intent?

0 Votes 0 ·

The "Acces" should contain the Token wich is just a Guid from the cookie, then it should pass it to the " [HttpGet("CardCount")]"

the "Cards" contains all the products that have be added to the card allong with the Cookie token so it should check all the
products that have been added to the card with the same acces token.

Example:
cookie.Default : 06a7fbee-4338-4308-8c11-db0affc12097

I have checked if react is reading the cookie correct with console.log and it wrote down the cookie token perfectly.


the problem is that for some reason asp net core is not able to recieve the acces token (the cookie) from Reactjs
I dont understand why it would not be able to read a token from a cookie


I dont use bearer tokens its just a database authorization that contains acces tokens

0 Votes 0 ·
osyris-3187 avatar image
0 Votes"
osyris-3187 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

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

 [HttpGet("CardCount")]
  public async Task<IActionResult> CardCount(string acces)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.