I am new to web api and I can't seem to send an object from the .net project through POST.
Here is the code to send the object:
public async Task<bool> CreateUser (User user)
{
Uri uri = new Uri(apiUrl + "api/users");
var jsonUser = JsonConvert.SerializeObject(user);
HttpContent content = new StringContent(jsonUser, Encoding.UTF8, "application/json");
var postUser = await client.PostAsync(uri, content);
string result = await postUser.Content.ReadAsStringAsync();
if (postUser.IsSuccessStatusCode)
{
return true;
}
return false;
}
And here is the web API code:
[HttpPost]
public IHttpActionResult Post([FromBody]string jsonUser)
{
UserDTO user = JsonConvert.DeserializeObject<UserDTO>(jsonUser);
bool tryCreate = _userRepository.CreateUser(user);
if (tryCreate == false)
{
return NotFound();
}
return Ok();
}
If I try to send a POST request to my web api url, it successfully sends the json string to the action, but if I try to send POST data from my web project, the Content.ReadAsStringAsync() returns:
"{"Message":"An error has occurred.","ExceptionMessage":"Value cannot be null.\r\nParameter name: value","ExceptionType":"System.ArgumentNullException","StackTrace":" at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)\r\n at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)\r\n at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)\r\n at Travue.WebAPI.Controllers.UsersController.Post(String jsonUser) in C:\\Users\\Dev01\\Documents\\projects\\travue\\Travue.WebAPI\\Controllers\\UsersController.cs:line 46\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.<GetExecutor>b__2(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()"}"