Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerate TWebRequest HTTP header fields

is it possible to dump all the header fields of a TWebRequest (and TWebResponse) object? At the moment I use GetFieldByName() and print them with Writeln() but this works only if I already know the name of the header field. I'm looking for a way to obtain all header field names to enumarate each field but I didn't find any method to do that.

I wrote a REST datasnap console application and wants to log all HTTP requests/responses to console.

like image 906
MBulli Avatar asked Dec 29 '11 10:12

MBulli


People also ask

What are HTTP header fields?

HTTP header fields are a list of strings sent and received by both the client program and server on every HTTP request and response. These headers are usually invisible to the end-user and are only processed or logged by the server and client applications.

What is content type in HTTP header?

The Content-Type representation header is used to indicate the original media type of the resource (prior to any content encoding applied for sending). In responses, a Content-Type header provides the client with the actual content type of the returned content.

How are header fields used?

The Content-Encoding entity-header field is used as a modifier to the media-type. When present, its value indicates what additional content codings have been applied to the entity-body, and thus what decoding mechanisms must be applied in order to obtain the media-type referenced by the Content-Type header field.


1 Answers

AFAIK it is not possibile (Delphi XE2).

I've used a little trink to have access to the raw headers. However, this is really dirty! Use at you own risk!

The actual class request class is the TIdHTTPAppRequest (WARNING: Could be different for different type of webbroker app. I''ve not tested this code with different kind of datasnap app).

So the trick is:

Declare a class helper similar to the following:

  TIdHTTPAppRequestHelper = class helper for TIdHTTPAppRequest
  public
    function GetRequestInfo: TIdEntityHeaderInfo;
  end;

  implementation

  function TIdHTTPAppRequestHelper.GetRequestInfo: TIdEntityHeaderInfo;
  begin
    Result := FRequestInfo;
  end;

In this way you can use this helper to have acccess to the protected FRequestInfo field.

In the OnAction event handler you can use the following code to have all the headers names:

procedure Twm.wmWebActionItem1Action(Sender: TObject; Request: TWebRequest;
                 Response: TWebResponse; var Handled: Boolean);
var
  HeadersCount: Integer;
  I: Integer;
  sw: TStreamWriter;
begin
  Response.ContentType := 'text/plain';
  Response.ContentStream := TMemoryStream.Create;
  sw := TStreamWriter.Create(Response.ContentStream);
  try
    HeadersCount := TIdHTTPAppRequest(Request).GetRequestInfo.RawHeaders.Count;
    for I := 0 to HeadersCount - 1 do
      sw.WriteLine(TIdHTTPAppRequest(Request).GetRequestInfo.RawHeaders.Names[I]);
  finally
    sw.Free;
  end;
  Handled := True;
end;

However, it is bad that TWebRequest do not allows to read the raw headers. That should be changed!

like image 52
Daniele Teti Avatar answered Sep 21 '22 02:09

Daniele Teti