C
C#2y ago
Ice_

❔ 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
Ice_
Ice_OP2y ago
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
Tvde1
Tvde12y ago
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
Ice_
Ice_OP2y ago
@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!
Tvde1
Tvde12y ago
it should deserialize the object for you but if it works, it works ^^
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server