ContentToStringlist (HttpResponse function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
 
(3 intermediate revisions by 2 users not shown)
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.
This method
<var>ContentToStringlist</var> does the following:
places into a Stringlist object the content returned
from an HTTPRequest <var>[[Get (HttpRequest function)|Get]]</var>, <var>[[Post (HttpRequest function)|Post]]</var>, or <var>[[Send (HttpRequest function)|Send]]</var> method.
ContentToStringlist does the following:
<ol>
<ol>
<li>Instantiates a Stringlist object.
<li>Instantiates a <var>Stringlist</var> object.
<li>Breaks the HTTPRequest 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.
Stringlist element in the Stringlist it created.
<li>Returns the <var>Stringlist</var>.
<li>Returns the Stringlist.
</ol>
</ol>
==Syntax==
==Syntax==
{{Template:HttpResponse:ContentToStringlist syntax}}
{{Template:HttpResponse:ContentToStringlist syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%lines</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>
<tr><th>%httpresp</th>
</table>
<td>A reference to an HTTPResponse object that was returned by a Get, Post, or Send method of an HTTPRequest object.
 
</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 <var class="product">User Language</var> 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 (HTTPRequest subroutine)|Print]]</var> methods:
<p class="code"> %myGradeBookResponse:contentToStringlist:print
<p class="code"> %myGradeBookResponse:contentToStringlist:print
</p>
</p>
Line 48: Line 33:


==Example==
==Example==
 
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.
The following code prompts for a URL,
<p class="code">begin
fetches the page at that URL, places it in a <var>Stringlist</var> using
   %httpRequest is object httpRequest
<var>ContentToStringlist</var>, and displays the lines.
   %httpResponse is object httpResponse
<p class="code"> begin
   %myPage is object stringlist
 
   %HTTPRequest is object HTTPRequest
   %HTTPResponse is object HTTPResponse
   %myPage is object Stringlist
   %i is float
   %i is float


   %HTTPRequest = new
   %httpRequest = [[New_(HttpRequest_constructor)|new]]
   %HTTPRequest:URL = $read('Hey Moe, give me a URL!')
   %httpRequest:[[URL_(HttpRequest_property)|url]] = $read('Hey Moe, give me a URL!')
   %HTTPResponse = %HTTPRequest:Get('XMLCLIENT', 0 )
   %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 48:
   end if
   end if


   if ( %HTTPResponse:Code = 200 ) then
   if ( %httpResponse:Code = 200 ) then
       %myPage = %HTTPResponse:ContentToStringlist
       %myPage = %httpResponse:contentToStringlist
       print %myPage:Count and 'lines obtained.'
       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 56:
   else
   else
       print 'Sorry dude, you got an error: ' and -
       print 'Sorry dude, you got an error: ' and -
         %HTTPResponse:statusline
         %httpResponse:[[StatusLine_(HttpResponse_function)|statusline]]
   end if
   end if
end
end
</p>
</p>
==See also==
==See also==
{{Template:HttpResponse:ContentToStringlist footer}}
<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}}

Latest revision as of 20:45, 10 October 2012

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:

  1. Instantiates a Stringlist object.
  2. Breaks the HttpRequest returned content into lines by scanning for line-ends.
  3. Translates each line from ASCII to EBCDIC, and places it in its own Stringlist element in the Stringlist it created.
  4. 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.