CleverComponents HttpServer - handling form-field requests

[Delphi] HttpServer - handling form-field requests

[Delphi] HttpServer - handling form-field requests


The code below runs the HTTP server, accepts requests from connected users, and sends responses. If the request contains form-field data, the form fields are parsed and extracted.

Download Source Code

procedure TForm1.clHttpServer1ReceiveRequest(Sender: TObject;
  AConnection: TclHttpUserConnection; const AMethod, AUri: string;
  AHeader: TclHttpRequestHeader; ABody: TStream);
var
  request: TclHttpRequest;
  user, password, response: string;
  field: TclFormFieldRequestItem;
begin
  request := TclHttpRequest.Create(nil);
  try
    //assign the request header
    request.Header := AHeader;

    //load and parse the request
    ABody.Position := 0;
    request.RequestStream := ABody;

    //extract the form fields
    field := request.FormFields['username'];
    if (field <> nil) then
    begin
      user := field.FieldValue;
    end;

    field := request.FormFields['password'];
    if (field <> nil) then
    begin
      password := field.FieldValue;
    end;

    //process the extracted fields and send a response
    response := '<html><body><p>The user logged in.</p></body></html>';
    (Sender as TclHttpServer).SendResponse(AConnection, 200, 'OK', response);
  finally
    request.Free();
  end;
end;


Read more:
https://www.clevercomponents.com/portal/kb/a170/httpserver-handling-form-field-requests.aspx


Download Clever Internet Suite v10.0 Full Source (April 29, 2021)

  • 227