REST API: What is it?
REpresentational State Transfer is what the acronym REST stands for. It's an architectural design that lays forth a set of guidelines for building Web services. REST recommends generating an object from the data a client requests and responding to the user with the object's values via a client-server conversation. For instance, you can construct an object on the server-side if the user requests a Bangalore taxi reservation at a specific location and time. You have an object over here, and you are transmitting the object's state. As a result, Representational State Transfer, or REST, is its name.
An application can be made more appropriate for the internet by utilizing the reduced bandwidth usage provided by the REST architectural style. It is entirely dependent on the resources and is frequently referred to as the "language of the internet."
REST API Fundamentals
The six REST guiding principles are listed below:
Without a state
The body of the request contains the current status of the resource, and the URL is used to identify the resource specifically. The client receives a response from the server via headers, body, or status once the request has been processed. All the necessary information is included in the requests that clients send to servers so that the servers can comprehend the requests. This may appear in the body, headers, query-string arguments, or even the URL. The server API does not cache any state, thus each request is independent of the others. Additionally helpful in scaling the API service in a cloud context is its RESTful characteristic.
Client-Server
The client-server architecture enables a uniform interface and separates clients from the servers. This enhances the portability across multiple platforms as well as the scalability of the server components.
Uniform Interface
To obtain the uniformity throughout the application, REST has the following four interface constraints:
- Resource identification
- Resource Manipulation using representations
- Self-descriptive messages
- Hypermedia as the engine of application state
Cacheable
To provide a better performance, the applications are often made cacheable. This is done by labeling the response from the server as cacheable or non-cacheable either implicitly or explicitly. If the response is denied as cacheable, then the client cache can reuse the response data for equivalent responses in the future.
Minimal APIs in .Net 8 or .Net 7
Minimal APIs are architected to create HTTP APIs with minimal dependencies. They are ideal for microservices and apps that want to include only the minimum files, features, and dependencies in ASP.NET Core.
Limitations of Minimal API
- No support for filters: For example, no support for IAsyncAuthorizationFilter, IAsyncActionFilter, IAsyncExceptionFilter, IAsyncResultFilter, and IAsyncResourceFilter.
- No support for model binding, i.e. IModelBinderProvider, IModelBinder. Support can be added with a custom binding shim.
- No support for binding from forms. This includes binding IFormFile. We plan to add support for IFormFile in the future.
- No built-in support for validation, i.e. IModelValidator
- No support for application parts or the application model. There's no way to apply or build your own conventions.
- No built-in view rendering support. We recommend using Razor Pages for rendering views.
- No support for JsonPatch
- No support for OData
- No support for ApiVersioning. See this issue for more details.
With the following APIs,
REST APIs follow standard HTTP Verbs like GET, POST, PUT, DELETE, PATCH which are basically CRUD operation on an object. The APIs are arranged to form an internet resource. For example in the above example, we have resource as “todoitem” which can be created, modified, deleted using APIs, and URL format is formed accordingly.
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext < TodoDb > (opt => opt.UseInMemoryDatabase("TodoList"));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapGet("/", () => "Hello World!");
app.MapGet("/todoitems", async (TodoDb db) => await db.Todos.ToListAsync());
app.MapGet("/todoitems/complete", async (TodoDb db) => await db.Todos.Where(t => t.IsComplete).ToListAsync());
app.MapGet("/todoitems/{id}", async (int id, TodoDb db) => await db.Todos.FindAsync(id)
is Todo todo ? Results.Ok(todo) : Results.NotFound());
app.MapPost("/todoitems", async (Todo todo, TodoDb db) => {
db.Todos.Add(todo);
await db.SaveChangesAsync();
return Results.Created($ "/todoitems/{todo.Id}", todo);
});
app.MapPut("/todoitems/{id}", async (int id, Todo inputTodo, TodoDb db) => {
var todo = await db.Todos.FindAsync(id);
if (todo is null) return Results.NotFound();
todo.Name = inputTodo.Name;
todo.IsComplete = inputTodo.IsComplete;
await db.SaveChangesAsync();
return Results.NoContent();
});
app.MapDelete("/todoitems/{id}", async (int id, TodoDb db) => {
if (await db.Todos.FindAsync(id) is Todo todo) {
db.Todos.Remove(todo);
await db.SaveChangesAsync();
return Results.Ok(todo);
}
return Results.NotFound();
});
app.Run();
class Todo {
public int Id {
get;
set;
}
public string ? Name {
get;
set;
}
public bool IsComplete {
get;
set;
}
}
class TodoDb: DbContext {
public TodoDb(DbContextOptions < TodoDb > options): base(options) {}
public DbSet < Todo > Todos => Set < Todo > ();
}
In summary
The REST architecture style offers a common method for clients to access and retrieve server data as well as a standard approach to arrange resources across the internet. Minimal APIs need to have the least amount of code possible and can be quickly and simply created without the need for authentication or with very little dependencies by sitting behind a gateway that does.
European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.