AddField (HttpRequest subroutine): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m ((start) matching syntax terms to syntax box (will need further review when syntax box fixed), edits, tags and links.)
Line 1: Line 1:
{{Template:HttpRequest:AddField subtitle}}
{{Template:HttpRequest:AddField subtitle}}
The <var>AddField</var> method lets you add a form field name and value pair to the HTTP request that is to be sent to the HTTP server.


This method lets you add a form field name and
value pair to the HTTP request that is to be sent to the HTTP server.
==Syntax==
==Syntax==
{{Template:HttpRequest:AddField syntax}}
{{Template:HttpRequest:AddField syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%httpreq</th>
<tr><th>httpRequest</th>
<td>A previously defined and instantiated <var>HttpRequest</var> object.
<td>A previously defined and instantiated <var>httpRequest</var> object.
</td></tr>
</td></tr>
<tr><th>fieldname</th>
<tr><th>fieldName</th>
<td>A string expression that names an HTTP form field. Field names are case insensitive.
<td>A string expression that names an HTTP form field. Field names are case insensitive.
Passing multiple occurrences is allowed: if a same-named field is already present, an additional instance is added.
Passing multiple occurrences is allowed; if a same-named field is already present, an additional instance is added.
</td></tr>
</td></tr>
<tr><th>fieldvalue</th>
<tr><th>fieldValue</th>
<td>A <var>Longstring</var> or a <var>Stringlist</var> object expression that results in a value for the field. If it is a <var>Stringlist</var>, a line-end sequence is appended to each <var>Stringlist</var> item as it is added to the Post data. The <var>[[LineEnd (HttpRequest property)|LineEnd]]</var> property sets which of three line-end sequence options is appended.
<td>A <var>[[Longstrings|Longstring]]</var> or a <var>[[Stringlist class|Stringlist]]</var> object expression that results in a value for the field. If it is a <var>Stringlist</var>, a line-end sequence is appended to each <var>Stringlist</var> item as it is added to the Post data. The <var>[[LineEnd (HttpRequest property)|LineEnd]]</var> property sets which of three line-end sequence options is appended.
</td></tr>
</td></tr>
<tr><th>file</th>
<tr><th>file</th>
Line 21: Line 20:
</td></tr>
</td></tr>
<tr><th>xlatflag</th>
<tr><th>xlatflag</th>
<td>This optional parameter controls whether the ''fieldvalue'' content gets EBCDIC-to-ASCII translation. If ''xlatflag'' is not specified, the default is <var>True</var> (use EBCDIC-to-ASCII translation). If the value is <var>False</var>, no translation occurs.
<td>This optional parameter controls whether the <var class="term">fieldValue</var> content is processed with EBCDIC-to-ASCII translation. If <var class="term">xlatflag</var> is not specified, the default is <var>True</var> (use EBCDIC-to-ASCII translation). If the value is <var>False</var>, no translation occurs.


</td></tr></table>
</td></tr></table>
==Usage notes==
==Usage notes==
<ul>
<ul>
<li>See <var>[[MultiPartFormEncoding (HttpRequest property)|MultiPartFormEncoding]]</var> for information about using <var>AddField</var>
<li>See <var>[[MultiPartFormEncoding (HttpRequest property)|MultiPartFormEncoding]]</var> for information about using <var>AddField</var> for a form used to Post file data.
for a form used to Post file data.
<li><var>AddField</var> is designed for requests that have the <code>application/x-www-form-urlencoded</code> or <code>multipart/form-data</code> content types. It is an error to use <var>AddField</var> in a request that has, say, a "text/xml" content type.
<li><var>AddField</var> is designed for requests that have the
<li>If form fields are defined with <var>AddField</var>, no explicit content type is set with <var>[[AddHeader (HttpRequest subroutine)|AddHeader]]</var>, and multipart form encoding is ''not'' enabled with
<code>application/x-www-form-urlencoded</code> or <code>multipart/form-data</code>
<var>[[MultiPartFormEncoding (HttpRequest property)|MultiPartFormEncoding]]</var>. the <var class="product">HTTP Helper</var> automatically sends the following request
content types.
It is an error to use <var>AddField</var> in a request that has, say, a "text/xml"
content type.
<li>If form fields are defined with <var>AddField</var>, no explicit content type is set
with <var>[[AddHeader (HttpRequest subroutine)|AddHeader]]</var>,
and multipart form encoding is ''not'' enabled with
<var>[[MultiPartFormEncoding (HttpRequest property)|MultiPartFormEncoding]]</var>.
the <var class="product">HTTP Helper</var> automatically sends the following request
header on Post calls:
header on Post calls:
<p class="code"> content-type: application/x-www-form-urlencoded
<p class="code"> content-type: application/x-www-form-urlencoded
</p>
</p>
<li>For information about <var>Stringlist</var> processing, see [[Stringlist class|"Stringlist class"]].
</ul>
</ul>


==Example==
==Example==
The following <var class="product">User Language</var> prompts for a procedure name, stores the procedure in a <var>Stringlist</var>, and uploads the procedure using multipart form encoding.<p class="code">begin
<ol>
   %request is object HttpRequest
<li>The following <var class="product">User Language</var> prompts for a procedure name, stores the procedure in a <var>Stringlist</var>, and uploads the procedure using multipart form encoding.
   %response is object HTTPResponse
<p class="code">begin
   %upfile is object <var>Stringlist</var>
   %request is object httpRequest
   %response is object httpResponse
   %upfile is object stringlist


   %procname is string len 255
   %procname is string len 255
Line 54: Line 46:


   %procname = $read('Enter proc name please')
   %procname = $read('Enter proc name please')
   %rc = $procopn( %procname )
   %rc = [[$ProcOpn|$procopn]]( %procname )
   if ( %rc ) then
   if ( %rc ) then
       print 'could not open the proc, dude.'
       print 'could not open the proc, dude.'
       stop
       stop
   end if
   end if
   %upfile = new
   %upfile = [[New_(Stringlist_constructor)|new]]
   %upfile:AppendOpenProcedure
   %upfile:[[AppendOpenProcedure_(Stringlist_function)|appendOpenProcedure]]
   %rc = $proccls
   %rc = [[$ProcCls|$proccls]]
   %upfile:print
   %upfile:[[Print_(Stringlist_function)|print]]
   %request = new
   %request = [[New_(HttpRequest_constructor)|new]]
   %request:URL = 'whatever'
   %request:URL = 'whatever'
   %request:MultiPartFormEncoding = true
   %request:multiPartFormEncoding = true
   %request:LineEnd = CR
   %request:lineEnd = CR
   %request:addField('filename',%upfile,%procname)
   %request:addField('filename',%upfile,%procname)
   %response = %request:post( 'CSOCK' )
   %response = %request:[[Post_(HttpRequest_function)|post]]( 'CSOCK' )
   print %response:statusline
   print %response:[[StatusLine_(HttpResponse_function)|statusline]]
   print %response:content
   print %response:[[Content_(HttpResponse_function)|content]]
end
end
</p>
</p></ol>
 
==See also==
==See also==
<ul>
<li>For information about <var>Stringlist</var> processing, see [[Stringlist class|"Stringlist class"]].
</ul>
{{Template:HttpRequest:AddField footer}}
{{Template:HttpRequest:AddField footer}}

Revision as of 06:31, 18 June 2011

Add a form field to the request (HttpRequest class)

The AddField method lets you add a form field name and value pair to the HTTP request that is to be sent to the HTTP server.

Syntax

httpRequest:AddField( fieldName, fieldValue, [file], [translate])

Syntax terms

httpRequest A previously defined and instantiated httpRequest object.
fieldName A string expression that names an HTTP form field. Field names are case insensitive.

Passing multiple occurrences is allowed; if a same-named field is already present, an additional instance is added.

fieldValue A Longstring or a Stringlist object expression that results in a value for the field. If it is a Stringlist, a line-end sequence is appended to each Stringlist item as it is added to the Post data. The LineEnd property sets which of three line-end sequence options is appended.
file A string that identifies the file to be uploaded. The default is no file specification, that is, a non-file data Post. This is an optional parameter.
xlatflag This optional parameter controls whether the fieldValue content is processed with EBCDIC-to-ASCII translation. If xlatflag is not specified, the default is True (use EBCDIC-to-ASCII translation). If the value is False, no translation occurs.

Usage notes

  • See MultiPartFormEncoding for information about using AddField for a form used to Post file data.
  • AddField is designed for requests that have the application/x-www-form-urlencoded or multipart/form-data content types. It is an error to use AddField in a request that has, say, a "text/xml" content type.
  • If form fields are defined with AddField, no explicit content type is set with AddHeader, and multipart form encoding is not enabled with MultiPartFormEncoding. the HTTP Helper automatically sends the following request header on Post calls:

    content-type: application/x-www-form-urlencoded

Example

  1. The following User Language prompts for a procedure name, stores the procedure in a Stringlist, and uploads the procedure using multipart form encoding.

    begin %request is object httpRequest %response is object httpResponse %upfile is object stringlist %procname is string len 255 %rc is fixed %procname = $read('Enter proc name please') %rc = $procopn( %procname ) if ( %rc ) then print 'could not open the proc, dude.' stop end if %upfile = new %upfile:appendOpenProcedure %rc = $proccls %upfile:print %request = new %request:URL = 'whatever' %request:multiPartFormEncoding = true %request:lineEnd = CR %request:addField('filename',%upfile,%procname) %response = %request:post( 'CSOCK' ) print %response:statusline print %response:content end

See also