I am trying to insert multiple users in database using a API. Now, suppose there are three users to be inserted and assume that one user didn't get inserted but other two are suceessfully inserted. So, I have a requirement to show response which shows that user first is successfully inserted , user 2nd have an error. That is why I have useed list of httpResponseMessage and in each httpresponsemessageobject, I will add complete user json indicating that this was the user, who have failed or success status
So, as per my implementation
I am using foreach loop to insert multiple users in db and returning reponse like this but my respose from api do not shows "user" object in content:
public async Task<List<HttpResponseMessage>> InserUsers(JArray obj)
{
List<myDTO> users = obj.ToObject<List<myDTO>>();
List<HttpResponseMessage> list = new List<HttpResponseMessage>();
foreach (var user in users)
{
var response = Insert(user);
list.Add(new HttpResponseMessage
{
Content = new StringContent(JsonConvert.SerializeObject(user), System.Text.Encoding.UTF8, "application/json"),
ReasonPhrase = response.ReasonPhrase,
StatusCode = response.StatusCode
});
}
return returnResposeList;
}
public HttpResponseMessage Insert(User user)
{
// do insert and if there is error
return new HttpResponseMessage()
{
StatusCode = HttpStatusCode.Error,
ReasonPhrase = $"Error"
};
else
{
return new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
ReasonPhrase = $"Successfully inserted"
};
}
}
Thanks & regards
List<HttpResponseMessage>? How is it used? You should spend a bit more time improving your question so that it can give us a clear picture of what the problem is.List<HttpResponseMessage>.