Understanding TWebRequest’s ContentFields and QueryFields with regard to MethodType¶
It seems rather obvious, but somehow the two are always being mixed up.
Thus, this will be 5 minutes well spent if you ever will implement a REST Web Service using
This content does not refer to TMS WEB Core
TMS WEB Core components use the
TWeb prefix. However, before that, a technology called DataSnap used it. This content refers to DataSnap. A web technology to write web servers and services quickly included with Delphi.
This article is kept so it is available for legacy projects. Please consider using TMS Sparkle, TMS XData, or another web server/service solution for new projects. In order to write Web applications, have a look at TMS WEB Core. On the bottom of this page you even find a book recommendation.
Considering the documentation
QueryFields refer to parameters in the URL and
ContentFields refers to the content of a
POST request message. Of course, both
POST requests can have
GET requests do not have
ContentFields. Both parameters are mapped to TStringList in Delphi. Multiple values can be separated with an ampersand (
This seems all to be pretty straight forward. Let’s see how it works in reality.
I wrote an example
POST request that has the following implementation:
procedure TModMain.WebModule1webSetResultsAction(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); begin Response.Content := '<html>' + '<body>' + 'QueryFields: ' + Request.QueryFields.Text + '<br/>' + 'ContentFields: ' + Request.ContentFields.Text + '</body>' + '</html>'; Handled := True; end;
POSTrequest to the URL
with the content
set up in the Fiddler composer…
…yields the following result:
Delphi gives us very easy access to parameters in URLs and the content of a
POST request this way. The
TWebRequest class also makes it possible to determine the method type of the request easily using
One might ask why that is necessary to check inside the method as the
TWebActionItem also has a MethodType and thus the event is only called for one method type. Well, there is a special method type.
The type is defined as follows:
You might implement one action event handler for all the different method types in Delphi and use
mtAny. Inside the event handler you will then determine the method type using the
MethodType property of the request.
One thing is very important to note: Accessing
ContentFields will not throw an exception or the property will not be nil either; instead the list will simply not contain any elements.
TMS WEB Core Book, 2nd Edition
If you want to learn more about TMS WEB Core, please have a look at my book that is designed for beginners that want to learn about Web development with Delphi as well as advanced developers that want to get the most out of TMS WEB Core. Examples in the book go in much more detail and as all basics are covered as well, you can always go back to read about it again. Something these tutorials simply cannot provide.