Professional Documents
Culture Documents
How To Make Your Web API Responses Consistent and Useful
How To Make Your Web API Responses Consistent and Useful
How To Make Your Web API Responses Consistent and Useful
Thinkstock
When working with ASP.NET Web API, it is important to return a consistent response for all the
requests that are processed by your API regardless of success or failure. This makes it a lot easier
to consume the API, without requiring complex code on the client. By using a custom wrapper
for Web API responses, you can ensure that all of your responses have a consistent structure, and
you can also include custom metadata. This article presents a discussion on how we can decorate
Web API responses with useful information.
Let's understand the problem we are trying to solve. In essence, your standard Web API response
will vary when executing different action methods, and we need a way to get around the
inconsistency of these responses. When you are trying to retrieve data using HTTP GET, the
response may contain data for one or more records. When you are executing a HTTP POST, the
response may not even include a body, but only a header. And when there is an error when
executing the request, the API will return an object with the appropriate error message.
The differences among all of these responses make it difficult to consume the API, because the
consumer needs to know the type and structure of the data that is being returned in each case.
Both the client code and the service code become difficult to manage.
We will now write a method that checks to see whether or not a response is valid. The following
method, IsResponseValid, returns true if the response is valid and false otherwise. We
will use this method in our custom delegating handler.
Nominations are open for the 2024 Best Places to Work in IT
You can retrieve the response content using the following code snippet and check if there is any
error.
object responseContent;
if(response.TryGetContentValue(out responseContent))
{
HttpError httpError = responseContent as HttpError;
if(httpError !=null)
{
errorMessage = httpError.Message;
statusCode = HttpStatusCode.InternalServerError;
responseContent = null;
}
}
The following code snippet shows how you can create an instance
of ResponseMetadata and initialize its properties with relevant information.
Note how the response metadata we just created has been passed in the second argument of
the CreateResponse method.
return result;
}
private bool IsResponseValid(HttpResponseMessage
response)
{
if ((response != null) && (response.StatusCode ==
HttpStatusCode.OK))
return true;
return false;
}
}
config.MessageHandlers.Add(new CustomResponseHandler());
And that’s it! You can now execute your Web API methods and see how the responses are
wrapped using the custom message handler we implemented.
In this article we implemented a custom message handler that was used to wrap Web API
responses to include additional metadata. Note that this was just a simple implementation. I will
leave it to you to add more features such as response headers, cookie information, etc. Happy
coding!
Source : https://www.infoworld.com/article/3674115/how-to-make-your-web-api-responses-
consistent-and-useful.html