frankcollins3 / ASPokedex

Pokedex with ASP backend / razor pages, and VanillaJS front end
1 stars 0 forks source link

415 unsupported media type [7:26am] #21

Closed frankcollins3 closed 12 months ago

frankcollins3 commented 12 months ago

attempting to do: first ASP.net POST route Screen Shot 2023-09-23 at 7 27 07 AM

error: Screen Shot 2023-09-23 at 7 26 15 AM

proposed approach: check the POST body ?

possible improvements:

frankcollins3 commented 12 months ago

first approach changed data to body

data: { 
            pokename: "squirtle",
            pokeid: "6",
            type: "water"
        },

[7:28am]

frankcollins3 commented 12 months ago

updated this code to include JSON.stringify(): Screen Shot 2023-09-23 at 7 33 57 AM

internal 500 server error: Screen Shot 2023-09-23 at 7 33 28 AM [7:34am]

frankcollins3 commented 12 months ago

tbh I still don't fully understand but some of that is becoming used to the ASP syntax:

    // POST: api/pokemon
    [HttpPost]
    public async Task<ActionResult<Pokemon>> AddPokemon([FromBody] Pokemon pokemon)
    {
        try
        {
            // Add the new Pokemon to the context
            _context.Pokemons.Add(pokemon);
            await _context.SaveChangesAsync();

            return CreatedAtAction(nameof(GetPokemon), new { id = pokemon.Id }, pokemon);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "An error occurred while adding a Pokemon.");
            return StatusCode(500, "A wild 500 error appears!");
        }
    }

[8:02am]