Question

Im trying to post some information in my api which is programmed using WCF Web Api. In the client, I'm using restsharp which is a rest client for restful services. However, when I try to post adding some parameters to the request, the post method in the service is never called, and my response object in the client gets a 500 status (internal server error), but when I comment the lines where I'm adding parameters the request reaches the post method exposed in the service.

Here's the code from the client:

[HttpPost]
    public ActionResult Create(Game game)
    {
        if (ModelState.IsValid)
        {
            var request = new RestRequest(Method.POST);
            var restClient = new RestClient();
            restClient.BaseUrl = "http://localhost:4778";
            request.Resource = "games";
            //request.AddParameter("Name", game.Name,ParameterType.GetOrPost); this is te line when commented     everything works fine
            RestResponse<Game> g = restClient.Execute<Game>(request);
            return RedirectToAction("Details", new {id=g.Data.Id });
        }
        return View(game);
    }

Here's the code for the service:

[WebInvoke(UriTemplate = "", Method = "POST")]
    public HttpResponseMessage<Game> Post(Game game, HttpRequestMessage<Game> request)
    {
        if (null == game)
        {
            return new HttpResponseMessage<Game>(HttpStatusCode.BadRequest);
        }
        var db = new XBoxGames();
        game = db.Games.Add(game);
        db.SaveChanges();

        HttpResponseMessage<Game> response = new HttpResponseMessage<Game>(game);
        response.StatusCode = HttpStatusCode.Created;

        var uriBuilder = new UriBuilder(request.RequestUri);
        uriBuilder.Path = string.Format("games/{0}", game.Id);
        response.Headers.Location = uriBuilder.Uri;
        return response;
    }

I need to add parameters to my request so the game object in the service gets populated, but I dont know how to do this, if the service breaks every time I try to add parameters.

I forgot to mention that both client and server are .NET MVC 3 applications.

Any help would be appreciate it. Thanks in advance.

Était-ce utile?

La solution

I noticed you are taking Game as a parameter and in HttpRequestMessage. You don't need to do this. Once you have the request (i.e. your request param), you can call ReadAs on the Content Property and you will get the Game instance. The fact that you are passing Game twice is probably causing the issue. Can you try removing your second game param and just use the one in the response?

WCF Web API does support form url encoding. In Preview 5, if you are using the MapServiceRoute extension method it will be automatically wired up. If you are not, then create a WebApiConfiguration object and pass it in to your ServiceHostFactory / ServiceHost.

Autres conseils

Well after going around this problem over and over again, I finally found a solution, however, I can't explain why this is happening.

I replaced the addParameter method for addBody, and everything worked as expected, I could post information on the server.

The problem seems to be that whenever I'm adding parameters through addParameter method, this method will append parameters as application/x-www-form-urlencoded, and apparently WCF web api is not supporting this type of data, and thats why it returns an internal server error to the client.

In contrary, addBody method uses text/xml which the server can understand.

Again, I don't know if this is whats actually happens, but it seems to be that.

This is how my client code looks now:

[HttpPost]        
    public ActionResult Create(Game game)
    {
        if (ModelState.IsValid)
        {
            RestClient restClient = new RestClient("http://localhost:4778");
            RestRequest request = new RestRequest("games/daniel",Method.POST);
            request.AddBody(game);
            RestResponse response = restClient.Execute(request);
            if (response.StatusCode != System.Net.HttpStatusCode.InternalServerError)
            {
                return RedirectToAction("Index");
            }
        }
        return View(game);

Please if you have any feedback, or know whats going on let me know.

I'm not familiar with the objects you're calling but is game.Name a string? If it's not, that might explain why AddParameter is failing.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top