When running the POST passing object via the URI of a Webapi, the object arrives as null in the method

Asked

Viewed 345 times

1

I am a few days beating head with this problem I have a fronted MVC5 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 Save method of the Webapi 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 tried to put before the object [Frombody] and [Fromuri] and nothing. my problem is sending objects to the URI to save or edit on order. When I call a JSON object listing for my frontend it’s OK. But to send not with you.

have I used

httpClient.Postasjsonasync httpClient.Postasync

If anyone can help me! I’m grateful!

Hugs!

1 answer

0


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

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

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

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

...or if you don’t want to change your method to async, make the calls this way not to cause the 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();

No need to use [FromBody] nor [FromUri] in your examples.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.