Q16. How to do API versioning. eg client 1 want to pass 2 parameters and client 2 want to pass 3 parameters.
Q17. What is content negotiation in webapi? How to achieve it?
Q18. How controller in mvc and webapi in .net core are different?
Q19. What is the difference between GET, POST, PUT, DELETE, PATCH ?
Q20. What are the features of REST api?
-----------------------------------------------------------------------------------------------------------------------------
Q16. How to do API versioning. eg client 1 want to pass 2 parameters and client 2 want to pass 3 parameters.
Answer:
There are multiple ways to handle API Versioning below are few of them.
1. URI Path versioning
2. URI Parameter Versioning
3. Content Negotiation
4. Request Header.
examples of URI path versioning
http://localhost:7500/api/v1/product/123
http://localhost:7500/api/v2/product/123
URI Parameter Versioning
http://localhost:7500/api/product/123?v=1.1
http://localhost:7500/api/product/123?v=1.2
-----------------------------------------------------------------------------------------------------------------------------
Q17. What is content negotiation in webapi? How to achieve it?
Answer:
Returning of data from API into various formats is known as content negotiation. it could be json, xml, plain text etc.
There are two main headers to achieve it.
Content-type: They of format data in going to API. example application/json Accept: The acceptable media types for the response, such as “application/json,” “application/xml,” This is type of format client is expecting.
-----------------------------------------------------------------------------------------------------------------------------
Q18. How controller in MVC and webapi in .net core are different?
Answer:
The Controller class derives from ControllerBase and adds some members that are needed to support Views.
Controller class = ControllerBase class + view support return types.
In .net core MVC controller get inherited from "Controller" class which as return type as JsonResult, ViewResult, PartialResult...
Where as webapi controller get inherited from ControllerBase. it has returntypes like ActionResult, Task <> type
-----------------------------------------------------------------------------------------------------------------------------
Q19. What is the difference between GET, POST, PUT, DELETE, PATCH ?
Answer:
When following REST guidelines for CRUD operations we have following options.
HTTP GET
HTTP POST
HTTP PUT
HTTP DELETE
HTTP PATCH
GET:
1. method is used to retrieve data from a server at the specified resource. it could be /users or users/324
2. HTTP Response 200(OK) - For any given HTTP GET API, if the resource is found on the server, then it must return HTTP response code 200 (OK) – along with the response body, which is usually either XML or JSON content (due to their platform-independent nature).
3. idempotent- Since a GET request is only requesting data and not modifying any resources, it's considered a safe and idempotent method.
Idempotence means that applying an operation once or applying it multiple times has the same effect. Examples: Multiplication by zero. No matter how many times you do it, the result is still zero.
POST:
1. POST requests are used to send data to the API server to create or update a resource. Request body could be JSON, XML, or query parameters.
2A. HTTP response code 201 (Created) -- the response SHOULD be HTTP response code 201 (Created) and contain an entity which describes the status of the request and refers to the new resource.
2B. It will create a new identity resource if the send data doesnot exits in database.
3. non-idempotent. It mutates data on the backend server (by creating or updating a resource), as opposed to a GET request which does not change any data.
PUT:
1. Similar to POST, PUT requests are used to send data to the API to update a resource. Usually whole object is send to request to make an update.
2. 200, 201, 204 -- when a PUT request creates a resource the server will respond with a 201 (Created), and if the request modifies existing resource the server will return a 200 (OK) or 204 (No Content)
3. idempotent. So if you send a request multiple times, that should be equivalent to single request modification. In contrast, calling a POST request repeatedly make have side effects of creating the same resource multiple times.
* The difference between the POST and PUT APIs can be observed in request URIs. POST requests are made on resource collections, whereas PUT requests are made on a single resource.
DELETE:
1. Delete the resource.
2. 200, 202 -A successful response of DELETE requests should be HTTP response code 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has been queued
3. idempotent - DELETE operations are idempotent. If you DELETE a resource, it’s removed from the collection of resources. Repeatedly calling DELETE API on that resource will not change the outcome – however, calling DELETE on a resource a second time will return a 404 (NOT FOUND) since it was already removed
PATCH:
1. make partial update on a resource. Only partial data is sent.
2. (Important) We must use patch in case of network utilization by sending less data over the network. So if we are using any cloud service where we are cost by network utilization we must implement PATCH.
3. It can only modify the data and not create it, unlike PUT which can create data as well in case data not found.
-----------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment