When executing POST passing object via URI of a WebAPI, the object arrives as null in the method

1

I have a few days of headaches with this problem I have an MVC5 fronted application and it sends an object to another WebAPI application in a Save () method.

See the code of my Frontend application that sends the object to the WebAPI

public bool Add(UsuarioDto model)
    {
        if (ModelState.IsValid)
        {
            using (var httpClient = new HttpClient())
            {
                var serializedObject = JsonConvert.SerializeObject(model,Formatting.Indented);
                var httpContent      = new StringContent(serializedObject, System.Text.Encoding.UTF8, "application/json");
                var uriWebApi        = String.Concat(UriBase, "Usuario/Save");

                var response = httpClient.PostAsJsonAsync(uriWebApi, serializedObject).Result;

                if (response.IsSuccessStatusCode)
                {
                    var task = response.Content.ReadAsAsync<UsuarioDto>();
                    task.Wait();
                    model = task.Result;
                }
            }

            return true;
        }
        else
        {
            TempData["Model"] = model;
            return false;
        }
    }

And here is the WebAPI Save method that receives the frontend object it arrives as empty or null.

[HttpPost]
    public UsuarioDto Save(UsuarioDto model)
    {
        try
        {
           var usuarioDomain = new Usuario();
            usuarioDomain.SaveAsync(model);
            return (model);
        }
        catch 
        {
            throw new HttpResponseException(HttpStatusCode.InternalServerError);
        }
    }

I've tried putting before the object [FromBody] and [FromUri] and nothing. my problem is being send objects to the URI save or edit in order. When I call an object listing in JSON to my frontend everything is OK. But to send I can not.

I've used it

link

If someone can help me! I'm grateful!

Hugs!

    
asked by anonymous 16.09.2017 / 19:52

1 answer

0

You must pass the original object to the call, instead of serializing manually, because the PostAsJsonAsync method itself will already serialize. The way you're showing it, it would serialize something that had already been serialized.

Also, avoid deadlocks in your code, transform your method into async and use await on all asynchronous calls:

var response = await httpClient.PostAsJsonAsync(uriWebApi, model);

if (response.IsSuccessStatusCode)
    model = await response.Content.ReadAsAsync<UsuarioDto>();

... or if you do not want to change your method to async , make calls in this way so as not to cause deadlocks:

var response = Task.Run(async() => await httpClient.PostAsJsonAsync(uriWebApi, model)).GetAwaiter().GetResult();

if (response.IsSuccessStatusCode)
    model = Task.Run(async() => await response.Content.ReadAsAsync<UsuarioDto>()).GetAwaiter().GetResult();

There's no need to use [FromBody] nor [FromUri] in your examples.

    
18.09.2017 / 15:31