Http verbs of web api



HTTP verbs.

HTTP verbs specify an action to be performed on a specific resource or a
collection of resources.

There are quite a few REST verbs available, but five of them are used
frequently. Such as GET, POST, PUT, PATCH and DELETE.

GET: Fetches a record or set of resources from the server.
POST: Creates a new set of resources or a resource.
PUT: Updates or replaces the given record.
PATCH: Modifies the given record.

Action Method Return Type


  • Void.
  • Primitive type or Complex type.
  • HttpResponseMessage.
  • IHttpActionResult.

Void

It's not necessary that all action methods must return something. It can have
void return type.

For example, consider the following Delete action method that just deletes
the student from the data source and returns nothing.

Primitive or Complex Type

An action method can return primitive or other custom complex types as
other normal methods.

HttpResponseMessage

Web API controller always returns an object of HttpResponseMessage to the
hosting infrastructure.

IHttpActionResult

The IHttpActionResult was introduced in Web API 2 (.NET 4.5). An action
method in Web API 2 can return an implementation of IHttpActionResult class
which is more or less similar to ActionResult class in ASP.NET MVC.

Types of status codes

API should strictly tell the client what exactly happened after the operation.
We concentrate on a few families of codes.


  • 2xx family (successful)
  • 3xx family (redirection)
  • 4xx family (client error)
  • 5xx family (server error)

2xx family (successful)

200 and 201 fall under the success family. They indicate that an operation
was successful.

200 (Successful Operation) is the most common type of response status code
in REST.

201 (Successfully Created) is returned when a POST operation successfully
creates a resource on the server.

3xx family (redirection)

These status codes are used to convey redirection messages. The most
important ones are 301 and 304.

301 is issued when a resource is moved permanently to a new URL endpoint.

The 304 status code indicates that content is cached and no modification
happened for the resource on the server.

4xx family (client error)

These are the standard error status codes which the client needs to interpret
and handle further actions. These have nothing to do with the server. A wrong
request format or ill-formed REST method can cause these errors. Of these,
the most frequent status codes API developers use are 400, 401, 403, 404,
and 405.

400 (Bad Request) is returned when the server cannot understand the
client request.

401 (Unauthorized) is returned when the client is not sending the
authorization information in the header.

403 (Forbidden) is returned when the client has no access to a certain
type of resources.

404 (Not Found) is returned when the client request is on a resource that
is no nexisting.

405 (Method Not Allowed) is returned if the server bans a few methods on
resources. GET and HEAD are exceptions.

5xx family (server error)

These are the errors from the server. The client request may be perfect, but
due to a bug in the server code, these errors can arise. The commonly used
status codes are 500, 501, 502, 503, and 504.

500 (Internal Server Error) status code gives the development error which is
caused by some buggy code or some unexpected condition.

501 (Not Implemented) is returned when the server is no longer supporting
the method on a resource.

502 (Bad Gateway) is returned when the server itself got an error response
from another service vendor.

503 (Service Unavailable) is returned when the server is down due to multiple
reasons, like a heavy load or for maintenance.

504 (Gateway Timeout) is returned when the server is waiting a long time for
a response from another vendor and is taking too much time to serve the
client.


#API #WEBAPI #CORE #SERVICE #C# #ASP.net

Post a Comment

Previous Post Next Post