ContentToStringlist (HttpResponse function): Difference between revisions
mNo edit summary |
m (match syntax table to syntax template; edits, tags and links) |
||
Line 1: | Line 1: | ||
{{Template:HttpResponse:ContentToStringlist subtitle}} | {{Template:HttpResponse:ContentToStringlist subtitle}} | ||
The <var>ContentToStringlist</var> method places into a <var>[[Stringlist_class|stringlist]]</var> object the content returned from an <var>[[HttpRequest class|HttpRequest]]</var> <var>[[Get (HttpRequest function)|Get]]</var>, <var>[[Post (HttpRequest function)|Post]]</var>, or <var>[[Send (HttpRequest function)|Send]]</var> method. | |||
places into a | |||
from an <var>[[HttpRequest class|HttpRequest]]</var> <var>[[Get (HttpRequest function)|Get]]</var>, <var>[[Post (HttpRequest function)|Post]]</var>, or <var>[[Send (HttpRequest function)|Send]]</var> method. | |||
<var>ContentToStringlist</var> does the following: | <var>ContentToStringlist</var> does the following: | ||
<ol> | <ol> | ||
<li>Instantiates a <var>Stringlist</var> object. | <li>Instantiates a <var>Stringlist</var> object. | ||
<li>Breaks the <var>HttpRequest</var> returned content into lines by scanning for line-ends. | <li>Breaks the <var>HttpRequest</var> returned content into lines by scanning for line-ends. | ||
<li>Translates each line from ASCII to EBCDIC, and places it in its own | <li>Translates each line from ASCII to EBCDIC, and places it in its own <var>Stringlist</var> element in the <var>Stringlist</var> it created. | ||
<var>Stringlist</var> element in the <var>Stringlist</var> it created. | |||
<li>Returns the <var>Stringlist</var>. | <li>Returns the <var>Stringlist</var>. | ||
</ol> | </ol> | ||
==Syntax== | ==Syntax== | ||
{{Template:HttpResponse:ContentToStringlist syntax}} | {{Template:HttpResponse:ContentToStringlist syntax}} | ||
===Syntax terms=== | ===Syntax terms=== | ||
<table class="syntaxTable"> | <table class="syntaxTable"> | ||
<tr><th>% | <tr><th>%sl</th> | ||
<td>A Stringlist object. | <td>A <var>Stringlist</var> object.</td></tr> | ||
<tr><th>httpResponse</th> | |||
<td>A reference to an <var>[[HttpResponse_class|HttpResponse]]</var> object that was returned by a <var>Get</var>, <var>Post</var>, or <var>Send</var> method of an <var>[[HttpRequest_class|HttpRequest]]</var> object. | |||
</td></tr> | </td></tr> | ||
</table> | |||
==Usage notes== | ==Usage notes== | ||
<ul> | <ul> | ||
<li>No single HTTPResponse content line may exceed 6124 bytes in length | <li>No single <var>HTTPResponse</var> content line may exceed 6124 bytes in length (excluding the line end), or the request is cancelled. The scanned-for line-ends are the three typical characters (<code>CRLF</code>, <code>CR</code>, <code>LF</code>). | ||
(excluding the line end), or the request is cancelled. | <li>This method is useful when the content returned from the HTTP server is a series of ASCII text lines separated by line-end characters (for example, an HTML document). Accessing such content with the <var>HTTPResponse</var> <var>[[Content (HttpResponse function)|Content]]</var> method would leave all the lines in one <var>Longstring</var>, which the application would have to separate into lines using string-handling User Language code. | ||
The scanned-for line-ends are the three typical characters (CRLF, CR, LF). | <li><var>ContentToStringlist</var> is also useful for getting readable output for debugging purposes. For example, you can string together the <var>ContentToStringlist</var> and <var>[[Print (HttpResponse subroutine)|Print]]</var> methods: | ||
<li>This method is useful when the content returned from the HTTP | |||
server is a series of ASCII text lines separated by line-end | |||
characters (for example, an HTML document). | |||
Accessing such content with the <var>HTTPResponse</var> | |||
<var>[[Content (HttpResponse function)|Content]]</var> method | |||
would leave all the lines in one <var>Longstring</var>, which the application | |||
would have to separate into lines using string-handling User Language code. | |||
<li><var>ContentToStringlist</var> is also useful for | |||
getting readable output for debugging purposes. | |||
For example, you can string together the <var>ContentToStringlist</var> and | |||
<var>[[Print (HttpResponse subroutine)|Print]]</var> methods: | |||
<p class="code"> %myGradeBookResponse:contentToStringlist:print | <p class="code"> %myGradeBookResponse:contentToStringlist:print | ||
</p> | </p> | ||
Line 48: | Line 33: | ||
==Example== | ==Example== | ||
<ol> | |||
The following code prompts for a URL, | <li>The following code prompts for a URL, fetches the page at that URL, places it in a <var>Stringlist</var> using <var>ContentToStringlist</var>, and displays the lines. | ||
fetches the page at that URL, places it in a <var>Stringlist</var> using | <p class="code">begin | ||
<var>ContentToStringlist</var>, and displays the lines. | %httpRequest is object httpRequest | ||
<p class="code"> begin | %httpResponse is object httpResponse | ||
%myPage is object stringlist | |||
% | |||
% | |||
%myPage is object | |||
%i is float | %i is float | ||
% | %httpRequest = [[New_(HttpRequest_constructor)|new]] | ||
% | %httpRequest:[[URL_(HttpRequest_property)|url]] = $read('Hey Moe, give me a URL!') | ||
% | %httpResponse = %httpRequest:[[Get_(HttpRequest_function)|get]]('XMLCLIENT', 0 ) | ||
if ( %httpResponse is null ) then | if ( %httpResponse is null ) then | ||
print 'Could not connect' | print 'Could not connect' | ||
Line 67: | Line 49: | ||
end if | end if | ||
if ( % | if ( %httpResponse:Code = 200 ) then | ||
%myPage = % | %myPage = %httpResponse:contentToStringlist | ||
print %myPage: | print %myPage:[[Count_(Stringlist_function)|count]] and 'lines obtained.' | ||
for %i from 1 to %myPage:count | for %i from 1 to %myPage:count | ||
print 'Line' and %i and ': ' and %myPage:item(%i) at 15 | print 'Line' and %i and ': ' and %myPage:item(%i) at 15 | ||
Line 75: | Line 57: | ||
else | else | ||
print 'Sorry dude, you got an error: ' and - | print 'Sorry dude, you got an error: ' and - | ||
% | %httpResponse:[[StatusLine_(HttpResponse_function)|statusline]] | ||
end if | end if | ||
end | |||
</p> | </p></ol> | ||
==See also== | ==See also== | ||
<ul> | <ul> | ||
<li>For information about returning response contents into a <var>Longstring</var>, | <li>For information about returning response contents into a <var>Longstring</var>, see <var>[[Content (HttpResponse function)|Content]]</var>. | ||
see <var>[[Content (HttpResponse function)|Content]]</var>. | |||
</ul> | </ul> | ||
{{Template:HttpResponse:ContentToStringlist footer}} | {{Template:HttpResponse:ContentToStringlist footer}} |
Revision as of 05:46, 19 June 2011
HTTP response data split into lines (HttpResponse class)
The ContentToStringlist method places into a stringlist object the content returned from an HttpRequest Get, Post, or Send method. ContentToStringlist does the following:
- Instantiates a Stringlist object.
- Breaks the HttpRequest returned content into lines by scanning for line-ends.
- Translates each line from ASCII to EBCDIC, and places it in its own Stringlist element in the Stringlist it created.
- Returns the Stringlist.
Syntax
%sl = httpResponse:ContentToStringlist
Syntax terms
%sl | A Stringlist object. |
---|---|
httpResponse | A reference to an HttpResponse object that was returned by a Get, Post, or Send method of an HttpRequest object. |
Usage notes
- No single HTTPResponse content line may exceed 6124 bytes in length (excluding the line end), or the request is cancelled. The scanned-for line-ends are the three typical characters (
CRLF
,CR
,LF
). - This method is useful when the content returned from the HTTP server is a series of ASCII text lines separated by line-end characters (for example, an HTML document). Accessing such content with the HTTPResponse Content method would leave all the lines in one Longstring, which the application would have to separate into lines using string-handling User Language code.
- ContentToStringlist is also useful for getting readable output for debugging purposes. For example, you can string together the ContentToStringlist and Print methods:
%myGradeBookResponse:contentToStringlist:print
The statement above produces much more readable output than this:
print %myGradeBookResponse:content
Example
- The following code prompts for a URL, fetches the page at that URL, places it in a Stringlist using ContentToStringlist, and displays the lines.
begin %httpRequest is object httpRequest %httpResponse is object httpResponse %myPage is object stringlist %i is float %httpRequest = new %httpRequest:url = $read('Hey Moe, give me a URL!') %httpResponse = %httpRequest:get('XMLCLIENT', 0 ) if ( %httpResponse is null ) then print 'Could not connect' stop end if if ( %httpResponse:Code = 200 ) then %myPage = %httpResponse:contentToStringlist print %myPage:count and 'lines obtained.' for %i from 1 to %myPage:count print 'Line' and %i and ': ' and %myPage:item(%i) at 15 end for else print 'Sorry dude, you got an error: ' and - %httpResponse:statusline end if end
See also
- For information about returning response contents into a Longstring, see Content.