ASP 02 HTTP
ASP 02 HTTP
HTTP is an application-protocol that defines a set of rules to send requests from a browser
to a server and send responses from the server to the browser.
Initially developed by Tim Berners Lee, later standardized by IETF (Internet Engineering Task
Force) and W3C (World Wide Web Consortium)
HTTP Response
Response Start Line
Includes HTTP version, status code and status description. HTTP Version: 1/1 | 2 | 3
Date: Date and time of the response. e.g: Tue, 15 Nov 1994 08:12:31 GMT
Cache-Control: Indicates number of seconds that the response can be cached at the browser.
e.g: max-age = 60
Accept: Represents MIME type of response content to be accepted by the client. e.g:
text/html
Date: Date and time of request. Eg: Tue, 15 Nov 1994 08:12:31 GMT
Post: Sends an entity object to server; generally, it will be inserted into the database.
Put: Sends an entity object to server; generally updates all properties (full-update) it in the
database.
Patch: Sends an entity object to server; generally updates few properties (partial-update) it in
the database.
Get:
Used to retrieve data from server.
Can send limited number of characters only to server. Max: 2048 characters
Used mostly as a default method of request for retrieving page, static files etc.
Post:
Used to insert data into server
Parameters will be in the request body (as query string, json, xml or form-data).
Notes
HTTP Protocol
HTTP (Hypertext Transfer Protocol) is a protocol used for transmitting hypertext (e.g.,
HTML) over the internet.
It operates on a client-server model, where the client (usually a web browser) makes
requests to a server, which then responds with the requested resources or error
messages.
Stateless Protocol: Each HTTP request is independent of others; the server does not
retain information from previous requests.
Request/Response Model:
Server Response: The server processes the request and sends back an HTTP
response.
HTTP Server
An HTTP server is software that handles HTTP requests from clients and serves back
responses. It processes incoming requests, executes the necessary logic (e.g., accessing a
database, generating HTML), and returns the appropriate response.
Examples: Apache HTTP Server, Nginx, Microsoft IIS, Kestrel (used with ASP.NET Core).
Kestrel:
Kestrel is a cross-platform web server included with ASP.NET Core.
It is lightweight, high-performance, and suitable for running both internal and public-
facing web applications.
1. Client Sends Request: The client (e.g., web browser) sends an HTTP request to the
server.
2. Kestrel Receives Request: Kestrel receives the request and passes it through the
ASP.NET Core middleware pipeline.
4. Generate Response: The application generates an HTTP response and sends it back
through the middleware pipeline.
5. Kestrel Sends Response: Kestrel sends the HTTP response back to the client.
How Browsers Use HTTP
Browsers use HTTP to request resources such as HTML documents, images, CSS files,
and JavaScript files from servers.
When a user enters a URL or clicks a link, the browser sends an HTTP request to the
server, which then responds with the requested resource.
Overview:
Status codes are issued by the server in response to the client's request to indicate the
result of the request.
Categories include:
2xx Success: The request was successfully received, understood, and accepted.
4xx Client Error: The request contains bad syntax or cannot be fulfilled.
5xx Server Error: The server failed to fulfill an apparently valid request.
201 Created: The request succeeded and a new resource was created.
204 No Content: The server successfully processed the request, but is not returning
any content.
400 Bad Request: The server could not understand the request due to invalid syntax.
403 Forbidden: The client does not have access rights to the content.
404 Not Found: The server cannot find the requested resource.
503 Service Unavailable: The server is not ready to handle the request.
Summary
HTTP Protocol: A fundamental protocol for web communication, following a
request/response model and operating statelessly.
HTTP Server: Software that processes HTTP requests and responses, such as Kestrel.
Browser Usage: Browsers request resources via HTTP, which are then processed and
rendered.
Dev Tools: Chrome Dev Tools can inspect HTTP traffic in detail.
Message Format: HTTP requests and responses consist of a start line, headers, and an
optional body.
Status Codes: Indicate the result of HTTP requests, categorized into informational,
success, redirection, client error, and server error codes.
Setting Status Codes and Headers: ASP.NET Core allows customization of responses
using code, enabling setting of status codes and headers as demonstrated.
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:
6. context.Response.Headers["Content-type"] = "text/html";
7. await context.Response.WriteAsync($"<p>{path}</p>");
8. await context.Response.WriteAsync($"<p>{method}</p>");
9. });
10. app.Run();
3. Writes the extracted path and method into the response body as HTML paragraphs.
Code 2: Handling GET Requests with Query Parameters
2. context.Response.Headers["Content-type"] = "text/html";
3. if (context.Request.Method == "GET") {
4. if (context.Request.Query.ContainsKey("id") {
5. string id = context.Request.Query["id"];
6. await context.Response.WriteAsync($"<p>{id}</p>");
7. } } });
This code: