All About API: HTTP Response Message - Part Three

In this article we will see HTTP Response Message as return type for the API method. This is part three of the article series. Before reading this article, I would recommend reading the following previous parts.

HTTPResponseMessage feature is provided by WebAPI framework which is used to create HTTP services. It helps us to send responses in different ways. HTTPResponse message is used to return data as well as some user friendly messages like:

 Status Code Status Message
 200  OK
 201  Created
 404  Not Found

Now Let’s see step by step implementation of HTTPResponseMessage:

  • Open Visual Studio Editor

  • Select ‘File’ menu, expand ‘New’ and click on ‘Project…’

    project

  • Expand ‘Visual C#’ and select ‘Web’ from the left panel
  • Select ‘ASP.NET Web Application’
  • Provide appropriate name of the application and select the location
  • Click on ‘OK’ button,

    ok

  • Select ‘Empty’ as a template and check ‘Web API’ check box
  • Press ‘OK’ button and it will create an empty Web API project.

    empty

  • Right click on ‘Controllers’, expand ‘Add’ and click on ‘Controller…’

    Controller

  • Select ‘Web API 2 Controller with read/write actions’ and press ‘Add’ button, which will add all HTTP methods.

    webapi

  • Provide appropriate name and press ‘Add’ button,

    add

  • Controller is created with read / write actions,

    1. namespace HTTPResponseMessage.Controllers  
    2. {  
    3.     public class ServiceController: ApiController  
    4.     {  
    5.         // GET: api/Service  
    6.         public IEnumerable < string > Get()  
    7.         {  
    8.                 return new string[]   
    9.                 {  
    10.                     "value1",  
    11.                     "value2"  
    12.                 };  
    13.             }  
    14.             // GET: api/Service/5  
    15.         public string Get(int id)   
    16.             {  
    17.                 return "value";  
    18.             }  
    19.             // POST: api/Service  
    20.         public void Post([FromBody] string value) {}  
    21.             // PUT: api/Service/5  
    22.         public void Put(int id, [FromBody] string value) {}  
    23.             // DELETE: api/Service/5  
    24.         public void Delete(int id) {}  
    25.     }  
    26. }  
  • Set HttpResponseMessage as return type for all API methods.

  • Here we are using Request.CreateErrorResponse to create an HttpResponseMessage. 
  1. namespace HTTPResponseMessage.Controllers  
  2. {  
  3.     public class ServiceController: ApiController  
  4.     {  
  5.         static List < string > serviceData = LoadService();  
  6.         public static List < string > LoadService()   
  7.         {  
  8.                 return new List < string > ()   
  9.                 {  
  10.                     "Mobile Recharge",  
  11.                     "Bill Payment"  
  12.                 };  
  13.             }  
  14.             // GET: api/Service  
  15.         public HttpResponseMessage Get()   
  16.             {  
  17.                 return Request.CreateResponse < IEnumerable < string >> (HttpStatusCode.OK, serviceData);  
  18.             }  
  19.             // GET: api/Service/5  
  20.         public HttpResponseMessage Get(int id)   
  21.             {  
  22.                 if (serviceData.Count > id) return Request.CreateResponse < string > (HttpStatusCode.OK, serviceData[id]);  
  23.                 else return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Item Not Found");  
  24.             }  
  25.             // POST: api/Service  
  26.         public HttpResponseMessage Post([FromBody] string value)   
  27.             {  
  28.                 serviceData.Add(value);  
  29.                 return Request.CreateResponse(HttpStatusCode.Created, "Item Added Successfully");  
  30.             }  
  31.             // PUT: api/Service/5  
  32.         public HttpResponseMessage Put(int id, [FromBody] string value)   
  33.             {  
  34.                     serviceData[id] = value;  
  35.                 return Request.CreateResponse(HttpStatusCode.OK, "Item Updated Successfully");  
  36.             }  
  37.             // DELETE: api/Service/5  
  38.         public HttpResponseMessage Delete(int id)   
  39.             {  
  40.             serviceData.RemoveAt(id);  
  41.             return Request.CreateResponse(HttpStatusCode.OK, "Item Deleted Successfully");  
  42.         }  
  43.     }  
  44. }  

Run your application and follow below steps:

GET

  • Select ‘GET’ as Method
  • Copy URL and press ‘SEND’ button
  • In the response header, you can see the http status code and message ‘200 OK’
  • Observe the response, we can see two string elements are added in the list.

    get

GET By ID

  • In order to get the individual string element from the list, pass index in the URL
  • In the response header, you can see the http status code and message ‘200 OK’
  • Check the response, you will have individual element associated with the index.

    getbyid

POST

  • Select ‘POST’ as method
  • Add Content-Type header with the value ‘application/json’
  • Add the value which you want to add in the list in Body and press ‘SEND’ button
  • In the response header, you can see the http status code and message ‘201 Created’

    post

PUT

  • Select ‘PUT’ as method and add index for which you want to modify in the URL
  • Add Content-Type header with value ‘application/json’
  • Add updated value in the body and press ‘SEND’ button
  • In the response header, you can see the http status code and message ‘200 OK’

    put

DELETE

  • Select ‘DELETE’ as method and add index for which you want to delete from the list in URL
  • Press ‘SEND’ button
  • In the response header, you can see the http status code and message ‘200 OK’.

    delete