Return bool value from web api

16,235

1) Can we return Boolean value true/false in HttpResponseMessage. I mean in CreateErrorResponse?

Technically you have following options

a) CreateErrorResponse will create HttpError object.

return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, false.ToString());

Serialized response will be

 {"Message":"False"}

b) Or you can

return Request.CreateResponse(HttpStatusCode.InternalServerError, false.ToString());

Response will be

"False"

2) What is best way to check if user get updated or not ? 3) Can this code be improved?

In my opinion, it is best to align your api with REST recommendation http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

Generally REST post api returns the resource created. Ex-

[HttpPost]
public HttpResponseMessage UpdateUserData(User user)
{                            
    try
    {
        // Validate user 
        // if invalid request return Request.CreateErrorResponse(HttpStatusCode.BadRequest, validationErrorMessage);

        //Code to change user
        user.Update();
        return Request.CreateResponse(HttpStatusCode.Created, user);
    }
    catch (Exception ex)
    {
        ExceptionLogger.LogException(ex);
        return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "error");
    }
}

If response is anything except 201, that means error creating user. Error details should be in message.

Share:
16,235
user1926138
Author by

user1926138

Updated on June 04, 2022

Comments

  • user1926138
    user1926138 almost 2 years

    I am using Web Api 2 to create 1 user. Below is code

    [HttpPost]
    public HttpResponseMessage UpdateUserData(User user)
    {                            
        try
        {
            //Code
            .
            .
            user.Update();
            return Request.CreateResponse(HttpStatusCode.OK, true);
        }
        catch (Exception ex)
        {
            ExceptionLogger.LogException(ex);
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "error");
        }
    }
    

    I trying to call this method from code behind like

    using (HttpClient client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://localhost:65486/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
        var response = client.PostAsJsonAsync("api/Stock/UpdateUserData", userObject).Result;
    
        if (response.IsSuccessStatusCode)
        {
            using (HttpContent content = response.Content)
            {
                Task<string> result = content.ReadAsStringAsync();
                string final = result.Result;
            }  
        }
        else
        {
            //code
        }
    }
    

    Now I have few queries

    • Can we return Boolean value true/false in HttpResponseMessage. I mean in CreateErrorResponse .

    • What is best way to check if user get updated or not ?

    • Can this code be improved?