Post (HttpRequest function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (misc cleanup)
 
(21 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{Template:HTTPRequest:Post subtitle}}
{{Template:HttpRequest:Post subtitle}}
The <var>Post</var> method sends an HTTP request to an HTTP server in the HTTP Post format: the request syntax sent to the server is the same as if a <code>METHOD="POST"</code> HTML form was submitted from a web browser.


The Post method sends an HTTP request to an HTTP server in the HTTP Post format:
For more information about the HTTP POST request format, and for information about the HTTP protocol, refer to http://www.w3.org/Protocols/rfc2616/rfc2616.html.
the request syntax sent to the server is the same as if a METHOD="POST" HTML form
was submitted from a web browser.


For more information about the HTTP POST request format, and for information
about the HTTP protocol, refer to http://www.w3.org/Protocols/rfc2616/rfc2616.html.
==Syntax==
==Syntax==
{{Template:HTTPRequest:Post syntax}}
{{Template:HttpRequest:Post syntax}}
 
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%httpresp</th>
<tr><th>%httpResponse</th>
<td>A reference to the HTTPResponse object instantiated and returned by this Post method.
<td>A reference to the <var>[[HttpResponse_class|HTTPResponse]]</var> object instantiated and returned by this <var>Post</var> method.
</td></tr>
</td></tr>
<tr><th>%httpreq</th>
 
<td>An instantiated HTTPRequest object that contains the request (its methods were used to create the request).
<tr><th>httpRequest</th>
<td>An instantiated <var>HttpRequest</var> object that contains the request (its methods were used to create the request).
</td></tr>
</td></tr>
<tr><th><var>Port=name</var></th>
 
<td>A string expression that identifies a previously defined Janus CLSOCK port. The ''name'' default is the <var>[[MASTER]]</var> port, if any is defined. However, if you omit ''name'' and no <var>MASTER</var> port is defined, the request is cancelled.
<tr><th><var>Port</var></th>
This argument is optional, but if you specify a value, the parameter name, <code>Port</code> (case not important), is [[Notation conventions for methods#Named parameters|allowed]] as a qualifier, as of <var class="product">Sirius Mods</var> version 7.2.
<td>A string expression that identifies a previously defined <var>CLSOCK</var> port. The <var class="term">string</var> default is the <var>[[MASTER]]</var> port, if any is defined. However, if you omit <var class="term">string</var> and no <var>MASTER</var> port is defined, the request is canceled.
<p>
This is an optional and [[Notation conventions for methods#Named parameters|name-allowed]] parameter.  If you specify a value, you may (but don't have to) specify the parameter name, <var>Port</var> (case not important). </p></td></tr>
 
<tr><th><var>Cancel</var></th>
<td>A numeric expression that controls whether the request is cancelled if a communication/socket error occurs. If zero, the request is not cancelled on these errors. If non-zero, the request is cancelled (though a message is still issued). If not specified, it defaults to 1 (cancel). See "Usage notes," below, for further discussion of error handling.
This is an optional and name-allowed parameter. If you specify a value, you may (but don't have to) specify the parameter name, <var>Cancel</var> (case not important) if you want.
 
<tr><th><var>CertificateCheck</var></th>
<td>This optional ([[Notation conventions for methods#Named parameters|name required]]) argument invokes an SSL certificate request from the server at the time of the method call.
The argument value (<var class="term">certificateChecker</var>) must be a function, presumably [[Local and Common entities#Defining and invoking a local method|local]], with this [[Method variables#Method template in method variable declaration|method template]]:           
<p class="syntax"><span class="literal">Function (String):</span><span class="term">methname</span> <span class="literal">Is Boolean</span> </p>                                   
<p>
When a connection is established, the passed function is called with the certificate (binary, ASN.1 encoded, content copied to a <var>Longstring</var>) as its method object. </p>
<ul>
<li>If the function returns a <code>True</code>, the request is completed. </li>
 
<li>If the function returns a <code>False</code>, the connection is closed and <var>HttpRequest</var> returns a null.
In this case, the request is canceled unless the <var>Cancel</var> argument setting prevents it. </li>
</ul>
<p>
For a <var>CertificateCheck</var> function example, see [[Get_(HttpRequest_function)#Examples|Examples]] for the <var>Get</var> method. </p></td></tr>
 
<tr><th><var>CheckKeepAliveCertificate</var></th>
<td>This optional, name required, argument controls how frequently a specified <var>CertificateCheck</var> function is invoked for a an HTTP [[HTTP Helper#Keep-Alive support|keep-alive]] connection. If <code>False</code>, the default, the <var class="term">certificateChecker</var> function is called only when the physical connection is first established and not for every HTTP request on that connection. If set to <code>True</code>, the <var>CertificateCheck</var> method is called for every request, keep-alive or not.  
</td></tr>
</td></tr>
<tr><th><var>Cancel=num</var></th>
</table>
<td>A numeric expression that controls whether the request is cancelled if a communication/socket error occurs. If zero, the request is not cancelled on these errors. If non-zero, the request is cancelled (though a message is still issued). If not specified, it defaults to 1 (cancel). See below for further discussion of error handling.
 
This argument is optional, but if you specify a value, the parameter name, <code>Cancel</code> (case not important), is allowed as a qualifier, as of <var class="product">Sirius Mods</var> version 7.2.
</td></tr></table>
==Usage notes==
==Usage notes==
<ul>
<ul>
<li>Two classes of errors can occur when <var>Post</var> is invoked:
<li>Two classes of errors can occur when <var>Post</var> is invoked:
<ul>
<ul>
<li>The HTTP request completed, but the server indicated a failure of the request
<li>The HTTP request completed, but the server indicated a failure of the request (an HTTP return code of 400 or greater). An example of this is the "404 not found" message issued by a web server when a non-existent document is referenced. In this class of error there was no TCP/IP communication error.
(an HTTP return code of 400 or greater).
An example of this is the "404 not found" message issued by a web server when a
non-existent document is referenced.
In this class of error there was no TCP/IP communication error.


For these errors, the request is never cancelled, and an <var>[[HTTPResponse class|HTTPResponse]]</var> object
For these errors, the request is never cancelled, and an <var>[[HttpResponse class|HttpResponse]]</var> object is always returned. The application handles these errors by [[HTTP Helper#The HttpResponse class|checking]] the <var>HTTPResponse</var> <var>[[Code_(HttpResponse_function)|Code]]</var>, <var>[[StatusLine_(HttpResponse_function)|StatusLine]]</var>, <var>[[Message_(HttpResponse_function)|Message]]</var>, and <var>[[Success_(HttpResponse_function)|Success]]</var> methods.
is always returned.
<li>While attempting the HTTP request, a socket or communication error occurred. Examples of such errors include failure to connect to the server host, and a communication failure on a TCP/IP send or receive.
The application handles these errors by [[The HTTPResponse methods#Status reporting|checking]] the
<var>HTTPResponse</var>
<var>Code</var>, <var>StatusLine</var>, <var>Message</var>, and <var>Success</var> methods.
<li>While attempting the HTTP request, a socket or communication error occurred.
Examples of such errors include failure to connect to the server host, and a
communication failure on a TCP/IP send or receive.


For this class of errors, no instance of <var>HTTPResponse</var> is created, and
For this class of errors, no instance of <var>HTTPResponse</var> is created, and a <var>Null</var> object is returned. In addition, <var>[[$Status]]</var> is set to 1, and <var>[[$StatusD]]</var> is set to one of these:
null is returned.
In addition, $STATUS is set to 1, and $STATUSD is set to one of these:
<ul>
<ul>
<li>A negative return code that indicates an error in connecting to the
<li>A negative return code that indicates an error in connecting to the target HTTP server. These codes are the same as those in [[Get (HttpRequest function)#Get method return values|Get method return values]]. </li>
target HTTP server.
 
These codes are the same as those in [[Get method return values|"Get method return values"]].
<li>A positive return code indicating a communications error after the connection was made. </li>
<li>A positive return code indicating a communications error after
the connection was made.
</ul>
</ul>
The request may or may not be cancelled based on the setting of
The request may or may not be cancelled based on the setting of the <var>Cancel</var> parameter.
the ''cancel'' parameter.
<p>
Some of these errors are relatively benign (for example, inability to connect because remote partner down, timeout value exceeded), so they produce information-only messages, and they are treated as non-counting.  These errors can still be detected and handled by the application (by checking for a <var>Null</var> result object, checking <var>$Status</var> and <var>$StatusD</var>, or both). </p>


Some of these errors are relatively benign (for example, inability to connect
<blockquote class="note">
because remote partner down, timeout value exceeded),
<p>'''Note:''' The distinctions between some of the return codes might or might not be meaningful, depending on the situation. For example, network problems can cause a 101 return code (indicating a timeout) or a 102 (indicating a connection closed).  Connecting to a server that is using a non-HTTP protocol can result in a 100 (invalid format data received from the server) or a 101 or 102, depending on the protocol used by the server to which the request is sent: a request sent to a telnet port '''usually''' results in a timeout (return code 101), while a request sent to an SSL port without using <code>https</code> to indicate SSL '''usually''' results in the target server breaking the connection (return code 102).</p>
so they produce information-only messages, and they are treated as non-counting.
<p>
These errors can still be detected and handled by the application (by
In general, it is risky to take action based on the distinction between the various non-zero error codes returned by this method.</p></blockquote>
checking for a <var>Null</var> result object, checking $STATUS and $STATUSD, or both).
</ul>
'''Note:'''
The distinctions between some of the return codes might or might not be meaningful,
depending on the situation.


For example, network problems can cause a 101 return code (indicating
For more information about an error, you may want to issue a <var>[[JANUS TRACE]]</var> command with a value of 8 or 15 for the <var>[[JANUS CLSOCK|CLSOCK]]</var> port that is handling your HTTP request.
a timeout) or a 102 (indicating a connection closed).
Connecting to a server that is using a non-HTTP protocol can result in a
100 (invalid format data received from the server) or a 101 or 102,
depending on the protocol used by the server to which the request is sent:
a request sent to a telnet port '''usually''' results in a
timeout (return code 101), while a request sent to an SSL port without using
<code>https</code> to indicate SSL '''usually''' results in the target
server breaking the connection (return code 102).


In general, it is risky to take action based on the
<li>If form fields are defined with <var>[[AddField (HttpRequest subroutine)|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 <var>Post</var> calls:
distinction between the various non-zero error codes returned by this method.
<p class="code">content-type: application/x-www-form-urlencoded
</ul>
</p>
<li>For HTTP Version 1.1 mode only, the HTTP request header "connection: close" is automatically sent on HTTP requests to ensure that the server closes the connection with the client when the HTTP request completes.  The HTTP Helper does '''not''' support persistent connections. </li>


For more information about an error, you may want to
<li>The HTTP request header "Content-Length" is automatically sent on POSTs, accompanied by the value of the number of bytes in the non-header posted data. </li>
issue a <var>[[JANUS TRACE command|JANUS TRACE]]</var> command.
with a value of 8 or 15
for the <var>[[JANUS CLSOCK|CLSOCK]]</var> port that is handling your HTTP request.


For more information about $STATUS and $STATUSD, see the <var class="product">Model 204</var>
<li>If the ports associated with the sockets in the HTTP request are not using SSL, and the <var>CertificateCheck</var> argument is specified, the <var class="term">certificateChecker</var> function does not get called. </li>
<i><b>User Language Manual:ehp3..
<li>If form fields are defined with <var>[[AddField (HTTPRequest subroutine)|AddField]]</var>,
no explicit content type is set
with [[AddHeader (HTTPRequest subroutine)|AddHeader]],
and multipart form encoding is ''not'' enabled with
<var>[[MultiPartFormEncoding (HTTPRequest property)|MultiPartFormEncoding]]</var>,
the HTTP Helper automatically sends the following request
header on Post calls:
<p class="code"> content-type: application/x-www-form-urlencoded
</p>
<li>For HTTP Version 1.1 mode only,
the HTTP request header &amp;amp;amp;ldquo;connection: close&amp;amp;amp;rdquo; is automatically
sent on HTTP requests to ensure that the server closes the
connection with the client when the HTTP request completes.
The HTTP Helper does '''not''' support persistent connections.
<li>The HTTP request header &amp;amp;amp;ldquo;Content-Length&amp;amp;amp;rdquo; is automatically
sent on POSTs, accompanied by the value of the number of bytes in the
non-header posted data.
<li>You can also send an HTTP POST using the <var>[[Send (HTTPRequest function)|Send]]</var> method.
</ul>
</ul>
==See also==
==See also==
{{Template:HTTPRequest:Post footer}}
<ul>
<li>You can also send an HTTP POST using the <var>[[Send (HttpRequest function)|Send]]</var> method.
</ul>
{{Template:HttpRequest:Post footer}}

Latest revision as of 20:29, 8 December 2015

Send an HTTP POST request to the server (HttpRequest class)

The Post method sends an HTTP request to an HTTP server in the HTTP Post format: the request syntax sent to the server is the same as if a METHOD="POST" HTML form was submitted from a web browser.

For more information about the HTTP POST request format, and for information about the HTTP protocol, refer to http://www.w3.org/Protocols/rfc2616/rfc2616.html.

Syntax

%httpResponse = httpRequest:Post[( [[Port=] string], [[Cancel=] number], - [CertificateCheck= certificateChecker], - [CheckKeepAliveCertificate= boolean])]

Syntax terms

%httpResponse A reference to the HTTPResponse object instantiated and returned by this Post method.
httpRequest An instantiated HttpRequest object that contains the request (its methods were used to create the request).
Port A string expression that identifies a previously defined CLSOCK port. The string default is the MASTER port, if any is defined. However, if you omit string and no MASTER port is defined, the request is canceled.

This is an optional and name-allowed parameter. If you specify a value, you may (but don't have to) specify the parameter name, Port (case not important).

Cancel A numeric expression that controls whether the request is cancelled if a communication/socket error occurs. If zero, the request is not cancelled on these errors. If non-zero, the request is cancelled (though a message is still issued). If not specified, it defaults to 1 (cancel). See "Usage notes," below, for further discussion of error handling.

This is an optional and name-allowed parameter. If you specify a value, you may (but don't have to) specify the parameter name, Cancel (case not important) if you want.

CertificateCheck This optional (name required) argument invokes an SSL certificate request from the server at the time of the method call.

The argument value (certificateChecker) must be a function, presumably local, with this method template:

Function (String):methname Is Boolean

When a connection is established, the passed function is called with the certificate (binary, ASN.1 encoded, content copied to a Longstring) as its method object.

  • If the function returns a True, the request is completed.
  • If the function returns a False, the connection is closed and HttpRequest returns a null. In this case, the request is canceled unless the Cancel argument setting prevents it.

For a CertificateCheck function example, see Examples for the Get method.

CheckKeepAliveCertificate This optional, name required, argument controls how frequently a specified CertificateCheck function is invoked for a an HTTP keep-alive connection. If False, the default, the certificateChecker function is called only when the physical connection is first established and not for every HTTP request on that connection. If set to True, the CertificateCheck method is called for every request, keep-alive or not.

Usage notes

  • Two classes of errors can occur when Post is invoked:
    • The HTTP request completed, but the server indicated a failure of the request (an HTTP return code of 400 or greater). An example of this is the "404 not found" message issued by a web server when a non-existent document is referenced. In this class of error there was no TCP/IP communication error. For these errors, the request is never cancelled, and an HttpResponse object is always returned. The application handles these errors by checking the HTTPResponse Code, StatusLine, Message, and Success methods.
    • While attempting the HTTP request, a socket or communication error occurred. Examples of such errors include failure to connect to the server host, and a communication failure on a TCP/IP send or receive. For this class of errors, no instance of HTTPResponse is created, and a Null object is returned. In addition, $Status is set to 1, and $StatusD is set to one of these:
      • A negative return code that indicates an error in connecting to the target HTTP server. These codes are the same as those in Get method return values.
      • A positive return code indicating a communications error after the connection was made.

      The request may or may not be cancelled based on the setting of the Cancel parameter.

      Some of these errors are relatively benign (for example, inability to connect because remote partner down, timeout value exceeded), so they produce information-only messages, and they are treated as non-counting. These errors can still be detected and handled by the application (by checking for a Null result object, checking $Status and $StatusD, or both).

      Note: The distinctions between some of the return codes might or might not be meaningful, depending on the situation. For example, network problems can cause a 101 return code (indicating a timeout) or a 102 (indicating a connection closed). Connecting to a server that is using a non-HTTP protocol can result in a 100 (invalid format data received from the server) or a 101 or 102, depending on the protocol used by the server to which the request is sent: a request sent to a telnet port usually results in a timeout (return code 101), while a request sent to an SSL port without using https to indicate SSL usually results in the target server breaking the connection (return code 102).

      In general, it is risky to take action based on the distinction between the various non-zero error codes returned by this method.

    For more information about an error, you may want to issue a JANUS TRACE command with a value of 8 or 15 for the CLSOCK port that is handling your HTTP request.

  • 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

  • For HTTP Version 1.1 mode only, the HTTP request header "connection: close" is automatically sent on HTTP requests to ensure that the server closes the connection with the client when the HTTP request completes. The HTTP Helper does not support persistent connections.
  • The HTTP request header "Content-Length" is automatically sent on POSTs, accompanied by the value of the number of bytes in the non-header posted data.
  • If the ports associated with the sockets in the HTTP request are not using SSL, and the CertificateCheck argument is specified, the certificateChecker function does not get called.

See also

  • You can also send an HTTP POST using the Send method.