HTTPRequest Event 840

Applies To: HTMLRenderer

Description

An HTTPRequest event is raised whenever the HTMLRenderer requests a url from the workspace. See InterceptedURLs property. The request could be generated by a form submission, clicking on a hyperlink, an AJAX request or a link to a resource like a style sheet, image or JavaScript file. An HTTPRequest event is also raised when the HTMLRenderer is initialised and both HTML and URL are empty.

The callback function must "fill in the blanks" in the event message and return the modified event message as its result.

The event message reported as the result of ⎕DQ, or supplied as the right argument to your callback function, is an 11-element vector as follows:

[1] Object ref or character vector
[2] Event 'HTTPRequest' or 840
[3]   'ProcessRequest' (unused)
[4] Handle Initially 0, must be set to 1.
[5] Status Integer HTTP status code (initially 0).
[6] Message Character vector containing the HTTP status message (initially empty).
[7] MIME Character vector containing the MIME type (initially empty). See below.
[8] URL Character vector containing the requested URL.
[9] Headers Character vector containing the HTTP Request headers (initially empty).
[10] Body Character vector containing the HTTP Request body (initially empty).
[11] Method Character vector containing the HTTP method, for example, 'GET' or 'POST' .

To process the request, the callback function should return the message with only the following items changed. Note that only elements [4 5 6 10] are always required, and it is important to set element [4] to 1.

[4] Handle 1
[5] Status Success is indicated by 200.
[6] Message Success is indicated by 'OK' .
[7] MIME Defaults to 'text/html' and need be specified only if the response (Body) is not a character vecttor containing HTML.
[9] Headers Not normally required.
[10] Body This may be a character vector or an integer numeric vector with values in the ranges ¯128-127 or 0-255. Note that a character vector will be prepended with a UTF-8 byte order mark (BOM). A numeric vector requires that [7] is an appropriate MIME type (for example, 'image/png' )

MIME types include:

  • text/html
  • text/css
  • text/plain
  • text/csv
  • application/javascript
  • application/xml
  • application/json

For a complete list of media/MIME types, see:

https://www.iana.org/assignments/media-types/media-types.xhtml:

Example

<!DOCTYPE html>
<html>
<head>
<Title>HTTPRequest Example</Title>
</head>
<body><h1>Simple Form</h1>

<form action="Hello">
  First name:<br>
  <input type="text" name="firstname" value="Mickey">
  <br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse">
  <br><br>
  <input type="submit" value="Submit">
</form>

<p>When you click the "Submit" button, the HTMLRenderer
 will fire an HTTPRequest event.</p>

</body>
</html>
      msgSayHello msg;url;names;first;last;response
[1]    url8msg
[2]    'Requested URL is: ',url
[3]    names(url'?&')url
[4]    first last{('=')}¨names
[5]    response'<!DOCTYPE html><html><head>'
[6]    response,'<Title>Hi Folks</Title>'
[7]    response,'</head><body>'
[8]    response,'<h1 align="center">Hello '
[9]    response,first,' ',last,'</h1>'
[10]   response,'</body></html>'
[11]   msg[4 5 6 10]1 200 'OK'response
     
      run
[1]    'hr'⎕WC'HTMLRenderer'
[2]    hr.Size25 25
[3]    hr.HTMLhtml
[4]    hr.onHTTPRequest'SayHello'
     
          run
Requested URL is: http://dyalog_root/Hello?firstname=Mickey&lastname=Mouse