HTTP Protocol
Overview:
Request/Response Model:
HTTP Server
Definition:
Examples:
Kestrel:
Request and Response Flow with Kestrel
How Browsers Use HTTP
Observing HTTP Requests and Responses in Chrome Dev Tools
HTTP Response Message Format
Response Message Format:
Example:
HTTP/1.1 200 OKContent-Type: text/htmlContent-Length: 137?<html><body><h1>Hello, World!</h1></body></html>
Commonly Used Response Headers:
Default Response Headers in Kestrel
HTTP Status Codes
Overview:
Common Status Codes:
Setting Status Codes and Response Headers in ASP.NET Core
Example Code 1:
var builder = WebApplication.CreateBuilder(args);var app = builder.Build();?app.Run(async (HttpContext context) =>{ context.Response.Headers["MyKey"] = "my value"; context.Response.Headers["Server"] = "My server"; context.Response.Headers["Content-Type"] = "text/html"; await context.Response.WriteAsync("<h1>Hello</h1>"); await context.Response.WriteAsync("<h2>World</h2>");});?app.Run();
Explanation:
Example Code 2:
csharpCopy codevar builder = WebApplication.CreateBuilder(args);var app = builder.Build();?app.Run(async (HttpContext context) =>{ if (1 == 1) { context.Response.StatusCode = 200; } else { context.Response.StatusCode = 400; } await context.Response.WriteAsync("Hello"); await context.Response.WriteAsync(" World");});?app.Run();
Explanation:
Summary
HTTP Requests
In the world of web applications, an HTTP request is a client's way of saying, "Hey server, I need something." This "something" could be a web page, an image, data from a database, or the result of some server-side calculation. The client, typically a web browser, sends this request to the server, which processes it and returns a response.
Anatomy of an HTTP Request
An HTTP request consists of several parts:
Query Strings: Passing Parameters in URLs
A query string is a way to pass parameters to a server within the URL itself. It starts with a question mark (?) and follows the path in the URL. Each parameter is a key-value pair, separated by an equals sign (=), and multiple parameters are separated by ampersands (&).
Example:
https://example.com/products?category=electronics&brand=apple
In this example, category=electronics and brand=apple are parameters being passed to the server.
The Request Object in ASP.NET Core
ASP.NET Core provides a HttpRequest object that gives you access to all the information within an incoming request. This object has properties like:
Code 1: Displaying Request Path and Method
var builder = WebApplication.CreateBuilder(args);var app = builder.Build();?app.Run(async (HttpContext context) =>{ string path = context.Request.Path; string method = context.Request.Method; context.Response.Headers["Content-type"] = "text/html"; await context.Response.WriteAsync($"<p>{path}</p>"); await context.Response.WriteAsync($"<p>{method}</p>");});?app.Run();
This code defines a simple middleware component (using app.Run) that:
Code 2: Handling GET Requests with Query Parameters
app.Run(async (HttpContext context) =>{ context.Response.Headers["Content-type"] = "text/html"; if (context.Request.Method == "GET") { if (context.Request.Query.ContainsKey("id")) { string id = context.Request.Query["id"]; await context.Response.WriteAsync($"<p>{id}</p>"); } }});
This code focuses on GET requests:
Code 3: Extracting the User-Agent Header
app.Run(async (HttpContext context) =>{ context.Response.Headers["Content-type"] = "text/html"; if (context.Request.Headers.ContainsKey("User-Agent")) { string userAgent = context.Request.Headers["User-Agent"]; await context.Response.WriteAsync($"<p>{userAgent}</p>"); }});
This code:
Summary about HTTP?Request:
HTTP requests are the messages sent from clients (like web browsers) to servers to request resources or actions. They consist of a start line (method, URI, HTTP version), headers (additional information), an empty line, and an optional body containing data. Query strings are used to pass parameters within URLs.
ASP.NET Core provides the HttpRequest object to access request details. The example codes demonstrated:
HTTP?Methods
GET: Retrieving Data
The GET method is primarily designed for fetching data from a server. Think of it as asking the server for a specific resource, like a webpage, an image, or some data from a database. Here's what characterizes GET requests:
Example GET Request:
GET /products?category=electronics&brand=apple HTTP/1.1Host: example.com
POST: Submitting Data
The POST method is primarily used for submitting data to the server for processing. This data is typically included in the body of the request and is not visible in the URL. Here's how POST requests differ from GET:
Example POST Request:
POST /login HTTP/1.1Host: example.comContent-Type: application/x-www-form-urlencoded?username=john&password=secret
Choosing Between GET and POST
Postman
Postman is a versatile API development and testing tool. It allows you to easily craft HTTP requests, send them to your ASP.NET Core application (or any API), and inspect the responses. It's a fantastic way to debug, experiment, and explore your API endpoints.
Installation
Usage: Making Requests to Your ASP.NET Core App
Let's say your ASP.NET Core application is running locally at https://localhost:7070 and has an endpoint /api/products. Here's how to use Postman:
Summary
HTTP (Hypertext Transfer Protocol):
HTTP Requests:
HTTP Responses: