Loading...
Home /  News 1

LATEST NEWS

We provide the best digital solutions for your company

How and why we use HttpResponseMessage in WebApi

It is an astonishing feature provided by WebAPI framework which makes us able to create HTTP services easily. This feature helps us to send back error messages in a variety of ways. In this post, I will go over some of these capabilities. When we talk about response messages in web API, we simply mean by receiving some info from the methods that we created in web API so that we can know what kind of response we will get. We can use HTTPResponseMessage to return data as well as some user friendly messages. Basic structure of HttpResponseMessage is as given below:

//GetUser By Id
Public HttpResponseMessage GetUser(int id)
{
//Do something here
}

Now, the question is why we use Response Messages when we are able to return data from the method directly or we can say that primitive types/complex types? Here is the simple get method that will return user.

Public HttpResponseMessage GetUser(int id)
{
return UserContext.Users.Where(u=>u.Id==id).FirstOrDefault();
}

The above method will fulfill our requirements, and it will return data according to id. But if there is no record belonging to id then it will return null with success message. So we can do one thing here that simply returns an error response code (404 ERROR) with message “USER NOT FOUND”. We can modify the above code something like below:

Public HttpResponseMessage GetUser(int id)
{
User user= UserContext.Users.Where(u=>u.Id==id).FirstOrDefault();
If(user==null)
{
return Request .CreateErrorResponse(HttpStatusCode.NotFound, ”User not found”);
}
else{
return Request .CreateResponse(HttpStatusCode.OK, user);
}
}

This is the example where we can actually see an action implemented that returns an error. The important point to note here is that we are using Request.CreateErrorResponse to create an HttpResponseMessage that wraps our custom error. Now the return type of above method is HttpResponseMessage. If we have any data which belongs to the provided id then we can get Response “HttpStatusCode.OK” and if no user exists then it will return a message like “HttpStatusCode.NotFound” i.e. “User not found”. There are many more types of ResponseMessages like “HttpStatusCode.Forbidden”, ”HttpStatusCode.Ambiguous”, ” HttpStatusCode.InternalServerError” etc. depending upon our need. We can use any message; there is almost every kind of ResponseMessages.

So, this is all about ResponseMessages in WEBAPI. I recommend to use the ResponseMessages whenever writing any method in API so that it will provide some more description about the data or response, we will have. From the programming point of view these messages also give the developers an idea about the returned response as to when it returns what kind of message/data by providing required conditions in the method. I will provide some more information later about different concepts available in WebAPI.

Benefits:

It is simple to use.
You can easily organize highly complex applications.
It helps to make a good use of HTTP Cache and Proxy Server which can handle high load.
Even if the application is not designed specifically it makes easy to use.