❔ Returning the user object that was just created after web api [POST]
I have a web API and the POST works just fine in Swagger. It does work fine if I use the web API from my website application but when I do a PostAsJsonAsync against api/User, I won't get the object back (so I can retrieve the new UserId).
I guess this is intended.
How would I make the POST method in the UserController to return the object back? A simple "return userobject;" wont suffice!
5 Replies
I fixed it. I had to have a JsonSerializer.Deserialize<User> in the create method of the service on my website. I thought I could just return the object back through a Post
return Ok(user)
or
return Created(...)
or the best way
return StatusCode(StatusCodes.201Created, user)
so you get a 201 but don't have to do the mandatory things in the created method
this means your endpoint should return a IActionResult
. Alternatively your endpoint can just return User
@Tvde1 I still had to use deserialization 🤔
I can simply return a User or use Ok(), Created() etc., it doesn't really matter. The user object did come back (noticed that when I read the httpresponsemessage today), I just had to deserialize it!
it should deserialize the object for you
but if it works, it works ^^
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.