| [Up][Next] |
The TRequest and TResponse classes are the foundation of HTTP communication in FCL-Web. TRequest represents incoming HTTP requests with methods, headers, parameters, and content. TResponse represents outgoing HTTP responses with status codes, headers, and content.
These classes provide comprehensive access to HTTP protocol features including custom headers, content types, query parameters, form data, and file uploads.
program RequestResponseExample; {$mode objfpc}{$H+} uses httpdefs, SysUtils, Classes; procedure ProcessRequest(Request: TRequest; Response: TResponse); var UserAgent, ContentType: String; Stream: TStringStream; begin Writeln('=== Processing HTTP Request ==='); // Read request information Writeln('Method: ', Request.Method); Writeln('URI: ', Request.URI); Writeln('Query String: ', Request.QueryString); Writeln('Content Length: ', Request.ContentLength); // Read headers UserAgent := Request.GetCustomHeader('User-Agent'); if UserAgent <> '' then Writeln('User Agent: ', UserAgent); // Read query parameters if Request.QueryFields.Values['search'] <> '' then Writeln('Search query: ', Request.QueryFields.Values['search']); // Read POST data (if any) if Request.ContentLength > 0 then begin Writeln('Request content: ', Request.Content); ContentType := Request.ContentType; Writeln('Content type: ', ContentType); end; // Process based on request method case Request.Method of 'GET': begin Response.Content := '{"message": "GET request processed"}'; Response.ContentType := 'application/json'; Response.Code := 200; end; 'POST': begin // Echo back the posted data Response.Content := 'Received: ' + Request.Content; Response.ContentType := 'text/plain'; Response.Code := 201; end; 'PUT': begin Response.Content := 'Resource updated'; Response.Code := 200; end; 'DELETE': begin Response.Content := ''; Response.Code := 204; // No Content end; else Response.Content := 'Method not allowed'; Response.Code := 405; end; // Set custom response headers Response.SetCustomHeader('X-Powered-By', 'FreePascal-FCL-Web'); Response.SetCustomHeader('X-Request-ID', 'req-' + IntToStr(Random(99999))); // Set security headers Response.SetCustomHeader('X-Content-Type-Options', 'nosniff'); Response.SetCustomHeader('X-Frame-Options', 'DENY'); Writeln('=== Response Generated ==='); Writeln('Status Code: ', Response.Code); Writeln('Content Type: ', Response.ContentType); Writeln('Content Length: ', Length(Response.Content)); end; procedure DemonstrateStreamContent(Response: TResponse); var Stream: TStringStream; begin Writeln; Writeln('=== Demonstrating Stream Content ==='); Stream := TStringStream.Create('{"users": ["alice", "bob", "charlie"]}'); try Response.ContentStream := Stream; Response.ContentType := 'application/json'; Response.FreeContentStream := False; // We manage the stream Writeln('Content set via stream'); Writeln('Stream size: ', Stream.Size, ' bytes'); finally Stream.Free; end; end; var Request: TRequest; Response: TResponse; begin Request := TRequest.Create; Response := TResponse.Create(Request); try // Simulate a GET request Request.Method := 'GET'; Request.URI := '/api/users'; Request.QueryString := 'search=john&limit=10'; Request.QueryFields.Values['search'] := 'john'; Request.QueryFields.Values['limit'] := '10'; Request.SetCustomHeader('User-Agent', 'TestClient/1.0'); ProcessRequest(Request, Response); // Simulate a POST request Writeln; Request.Method := 'POST'; Request.URI := '/api/users'; Request.Content := '{"name": "John Doe", "email": "john@example.com"}'; Request.ContentType := 'application/json'; ProcessRequest(Request, Response); // Demonstrate stream content DemonstrateStreamContent(Response); finally Response.Free; Request.Free; end; end.