Action/Operations in Web API

When we talk about a database table row, these are the following 4 actions that can be performed on the row
- Create a row
- Read a row
- Update a row
- Delete a row

In the context of an ASP.NET Web API, these 4 actions correspond to GET, POST, PUT and DELETE as shown in the table below

CRUD

HTTP Verb

Create

POST

Read

GET

Update

PUT

Delete

DELETE

Terms & Concepts in Web API

  • Request Verbs: HTTP verbs (GET, POST, PUT & DELETE) describe what should be done with the resource. For example do you want to create, read, update or delete an entity. GET, PUT, POST and DELETE http verbs are the most commonly used one's                                                     
  • Request Header: When a client sends request to the server, the request contains a header and a body. The request header contains additional information such as what type of response is required. For example, do you want the response to be in XML or JSON.                                                  
  • Request Header: When a client sends request to the server, the request contains a header and a body. The request header contains additional information such as what type of response is required. For example, do you want the response to be in XML or JSON.                                                  
  • Request Header: When a client sends request to the server, the request contains a header and a body. The request header contains additional information such as what type of response is required. For example, do you want the response to be in XML or JSON.                                                  
  • Request Header: When a client sends request to the server, the request contains a header and a body. The request header contains additional information such as what type of response is required. For example, do you want the response to be in XML or JSON.                                                  
  • Request Body: Request Body contains the data to send to the server. For example, a POST request contains the data for the new item that you want to create. The data format may be in XML or JSON.                                                                                                                                             
  • Response Body: The Response Body contains the data sent as response from the server. For example, if the request is for a specific product, the response body includes product details either in XML or JSON format.                                                                                                                                                      
  • Response Status codes: These are the HTTP status codes, that give the client details on the status of the request. Some of the common status codes are 200-OK, 404-Not Found, 204-No Content.                                                                                                                                                       

We can a use a tools like fiddler, Postman to perform POST, PUT & DELETE actions.


Web API Content Negotiation

Depending on the Accept header value in the request, the Web API server sends the response. This is called Content Negotiation. If you don't specify the Accept header, by default the Web API returns JSON data.


Setting Formatter - 
Accept: application/xml
Accept: application/json

We can change the serialization settings of these formatters. 
e.g., Following configuration properly indentd data and use camel case instead of pascal case for property names. Changes in in WebApiConfig.cs file in App_Start folder.

config.Formatters.JsonFormatter.SerializerSettings.Formatting =
                            Newtonsoft.Json.Formatting.Indented;

config.Formatters.JsonFormatter.SerializerSettings.ContractResolver =
    new CamelCasePropertyNamesContractResolver();



Comments