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.
 
See also:
 
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;

Add Feedback