TsgcWebSocketServer_HTTPAPI | Send Text Response
Use the event OnHTTPRequest to handle the HTTP Requests.
The class THttpServerRequest contains the HTTP Request Data.
- Document: the Document the peer is trying to access.
- Method: the HTTP Method ('GET', POST'...)
- Headers: the Headers of HTTP request.
- AcceptEncoding: accept encoding variable, example: "gzip, deflate, br".
- ContentType: example: "text/html"
- Content: content of request if exists.
- QueryParams: the query parameters.
- Cookies: the cookies if exists.
- ContentLength: size of the content.
- AuthExists, AuthUsername, AuthPassword: authentication request data.
- Stream: if the http request has a body, this is the stream of the body.
The class THttpServerResponse contains the HTTP response Data.
- ContentText: is the response as text.
- ContentType: example: "text/html". If you want encode the ContentText with UTF8, set the charset='utf-8'. Example: text/html; charset=utf-8
- CustomHeaders: if you need to send your own headers use this variable
- AuthRealm: if the server requires authentication, set this variable.
- ResponseNo: the HTTP response number, example: 200 means the response if correct.
- ContentStream: if the response contains a stream, set here (don't free the stream, it will be freed automatically).
- FileName: if the response is a filename, set here the full path to the filename.
- Date, Expires, LastModified: datetime variables of the response.
- CacheControl: allows customizing the cache behaviour.
Example: if the server receives a GET request to the document "/test.html" send a OK response, otherwise send a 404 if it's a GET request or error 500 if it's another method.
procedure OnHTTPRequest(TsgcWSConnection_HTTPAPI *aConnection,
const THttpServerRequest *aRequestInfo,
var THttpServerResponse *aResponseInfo)
{
if (aRequestInfo->Method == "GET")
{
if aRequestInfo->Document == '/test.html' then
{
aResponseInfo->ResponseNo = 200;
aResponseInfo->ContentText = "OK";
aResponseInfo->ContentType = "text/html; charset=UTF-8";
}
else
{
aResponseInfo->ResponseNo = 404;
}
}
else
{
aResponseInfo->ResponseNo = 500;
}
}