SkyeBoniwell
SkyeBoniwell

Reputation: 7092

trying to use POST, but API tries to use GET

I'm trying to get an api method to use POST but it insists on trying to use GET. I've Googled and searched SO, but everything I try just returns the same message.

Here's my controller:

    [Route("api/game/{gameId}/createcharacter/{name}")]

    [AcceptVerbs("POST")]
    [HttpPost]
    public IHttpActionResult PostNewCharacter([FromBody]string name)
    {
        return Created("created", CharacterGenerationService.CreateNewCharacter(name));
    }

Here's the message I get no matter what I try:

message: "The requested resource does not support http method 'GET'."

Request URL:http://localhost:61645/api/game/452/createcharacter/testChar1 Request Method:GET Status Code:405 Method Not Allowed Remote Address:[::1]:61645

I am using : using System.Web.Http;

Is there a trick to this?

Thanks

Upvotes: 2

Views: 109

Answers (2)

Karthik M R
Karthik M R

Reputation: 990

use this code:

[Route("api/game/{gameId}/createcharacter/{name}")]
[HttpPost]
public IHttpActionResult PostNewCharacter(string name)
{
    return Created("created", CharacterGenerationService.CreateNewCharacter(name));
}

Upvotes: 2

CodeNotFound
CodeNotFound

Reputation: 23190

In the route template you said you will get the name parameter value from the URL but in the signature of your method your decorate that same parameter with FromBody attribute which tell that the value will be retrieved from the message body.

You have two choices :

  • remove the FromBody attribute and your URL will work correctly
  • or remove the {name} parameter in your route template and post the name value into your request body.

What about the gameId parameter ? Don't you need it in your method ? If yes, then you need to pass it as a method parameter.

Upvotes: 0

Related Questions