Changes

Jump to navigation Jump to search
1,819 bytes added ,  19:58, 16 February 2018
no edit summary
This module contains an [[Querying#FunctionsModule Library|XQuery functionModule]] contains a single function to send HTTP requests and handle HTTP responses. The function <code>{{Code|send-request<}} is based on the [http://expath.org/spec/code>http-client EXPath HTTP Client Module]. It gives full control over the available request and response parameters. For simple GET requests, which is introduced with the [[Fetch Module]] may be sufficient. With {{Version|9.0}}, if <code><http:header name="Accept-Encoding" value="gzip"/></code> prefixis specified and if the addressed web server provides support for the {{Code|gzip}} compression algorithm, is linked the response will automatically be decompressed. =Conventions= All functions in this module are assigned to the statically declared <code><nowiki>http://expath.org/ns/http-client</nowiki></code> namespace and based on , which is statically bound to the {{Code|http}} prefix.<br/>All errors are assigned to the [<code><nowiki>http://expath.org/specns/error</nowiki></http-client EXPath HTTP Client Module]:code> namespace, which is statically bound to the {{Code|exerr}} prefix. =Functions=
==http:send-request==
 {|width='100%'
|-
| valign='top' width='90120' | '''Signatures'''|<code><b>{{Func|http:send-request</b>(|$request as element(http:request)?, $href as xs:string?, $bodies as item()*) as |item()+</code>}}<br/><code><b>{{Func|http:send-request</b>(|$request as element(http:request)) as |item()+</code>}}<br /><code><b>{{Func|http:send-request</b>(|$request as element(http:request)?, $href as xs:string?) as |item()+</code>}}<br />
|-
| valign='top' | '''Summary'''|Sends an HTTP request and interprets the corresponding response. <code>{{Code|$request</code> }} contains the parameters of the HTTP request such as HTTP method and headers. In addition to this it can also contain the URI to which the request will be sent and the body of the HTTP method. If the URI is not given with the parameter <code>{{Code|$href</code>}}, its value in <code>{{Code|$request</code> }} is used instead. <br/>The structure of <code>{{Code|http:request</code> }} element follows the [http://expath.org/spec/http-client EXPath] specification. Both basic and digest authentication is supported.
|-
| valign='top' | '''NotesErrors'''|The attribute {{Error|HC0001|#Errors}} an HTTP error occurred.<br/>{{Error|HC0002|#Errors}} error parsing the entity content as XML or HTML.<codebr/>auth{{Error|HC0003|#Errors}} with a multipart response, the override-media-type must be either a multipart media type or application/octet-methodstream.<br/code> of {{Error|HC0004|#Errors}} the src attribute on the body element is mutually exclusive with all other attribute (except the media-type).<codebr/>${{Error|HC0005|#Errors}} the requestelement is not valid.<br/code> is not considered in our implementation because we are handling only basic authentication{{Error|HC0006|#Errors}} a timeout occurred waiting for the response.
|}
==Examples==
'''Example 1: ==Status Only==Simple GET request. Attribute <code>As the attribute {{Code|status-only</code> }} is set to true and that is why , only the response element is returned, no content.'''
'''Query:'''
</http:response></pre>
'''Example 2: ==Google Homepage== Retrieve the Google search home pagewith a timeout of 10 seconds. In order to [[http://home.ccil.org/~cowan/XML/tagsoup/ Parsers#HTML_Parser|parse HTML]], TagSoup] must be referenced contained in the class path in order to parse html.'''
'''Query:'''
<pre class="brush:xquery">http:send-request(<http:request method='get' href='http://www.google.com' timeout='10'/>)</pre>
'''Result:'''
<pre class="brush:xml">
<http:header name="Expires" value="-1"/>
<http:header name="X-XSS-Protection" value="1; mode=block"/>
<http:header name="Set-Cookie" value="NID=44=Hb575zZBVz3JsZmk7JTwpX7WQ7VODk-KQmbtyDnLawiHB7sIEScdRBD9apIqR8VjH1MexPV4OABBdr1CBm0Ku-1bUncC-v1XAVYql85IoyQfx1zJiFyWCZdIC9B22jV1...; expires=Tue, 13-Sep-2011 22:03:25 GMT; path=/; domain=.google.ch; HttpOnly"/>
<http:header name="Content-Type" value="text/html; charset=ISO-8859-1"/>
<http:header name="Server" value="gws"/>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"/>
<title>Google</title>
<script>window.google={kEI:"rZB-TcL0A4Ov8QOx6YHLCA",kEXPI:"28501,29014,29135,29265,29279",kCSI:{e:"28501,29014,29135,29265,29279",ei:"rZB-TcL0A4Ov8QOx6YHLCA",expi:"28501,29014,29135,29265,29279"},ml:function(){},kHL:"de",time:function(){return(new Date).getTime()},log:function(c,d,b){var a=new Image,e=google,g=e.lc,f=e.li;a.onerror=(a.onload=(a.onabort=function(){delete g[f]}));g[f]=a;b=b||"/gen_204?atyp=i&amp;ct="+c+"&amp;cad="+d+"&amp;zx="+google.time();a.src=b;e.li=f+1},lc:[],li:0,Toolbelt:{}};............google.timers.load.t)return;google.timers.load.t.ol=(new Date).getTime();google.timers.load.t.iml=f;google.kCSI.imc=d;google.kCSI.imn=b;google.kCSI.imp=e;google.timers.load.t.xjs&amp;&amp;google.report&amp;&amp;google.report(google.timers.load,google.kCSI)}if(window.addEventListener)window.addEventListener("load",l,false);else if(window.attachEvent)window.attachEvent("onload",l);google.timers.load .t.prt=(f=(new Date).getTime());})(); </script>
</center>
</body>
</html>
</pre>
 The response content type can also be overwritten in order to retrieve HTML pages and other textual data as plain string (using {{Code|text/plain}}) or in its binary representation (using {{Code|application/octet-stream}}). With the {{Code|http:header}} element, a custom user agent can be set. See the following example: '''Query:'''<pre class="brush:xquery">let $binary := http:send-request( <http:request method='get' override-media-type='application/octet-stream' href='http://www.google.com'> <http:header name="User-Agent" value="Opera"/> </http:request>)[2]return try { html:parse($binary)} catch * { 'Conversion to XML failed: 'Example 3|| $err: Example with content description}</pre> ===SVG Data=== Content-type ending with +xml, e.g. image/svg+xml.'''
'''Query:'''
'''Result:'''
<pre class="brush:xqueryxml"><http:response status="200" message="OK">
<http:header name="ETag" value="W/&quot;11b6d-4ba15ed4&quot;"/>
<http:header name="Age" value="9260"/>
<stop stop-color="#3f3fff" stop-opacity="0" offset="1"/>
</linearGradient>
<linearGradient id="lg1"> <stop stop-color="#111111" offset="0"/> <stop stop-color="#111111" stop-opacity="0" offset="1"/> </linearGradient>............72835 98.448425,358.72835 98.448425,357.52606 98.448425,356.32376 98.448425,355.12147 103.25761,355.12147 108.06679,355.12147 112.87597,355.12147 112.87597,356.32376 112.87598,357.52606 112.87598,358.72836 115.28058,358.72836 117.68516,358.72835 120.08975,358.72835 120.08975,363.53754 120.08975,368.34672 120.08975,373.1559 118.88745,373.1559 117.68516,373.1559 116.48286,373.1559 116.48286,369.54902 116.48286,365.94213 116.48286,362.33524 z M 84.020882,358.72836 84.02088,355.12147 94.841547,355.12147 94.841547,358.72836 84.020882,358.72836 z" fill="#030303"/>
</svg></pre>
 ==POST Request== POST request to the BaseX REST Service, specifying a username and password. '''Query:'''<pre class="brush:xquery">let $request := <http:request href='http://localhost:8984/rest' method='post' username='admin' password='admin' send-authorization='true'> <http:body media-type='application/xml'> <query xmlns="http://basex.org/rest"> <text><![CDATA[Category <html>{ for $i in 1 to 3 return <div>Section {$i }</div> }</html> ]]></text> </query> </http:body> </http:request>return http:send-request($request)</pre> '''Result:'''<pre class="brush:xml"><http:XQueryresponse xmlns:http="http://expath.org/ns/http-client" status="200" message="OK"> <http:header name="Content-Length" value="135"/> <http:header name="Content-Type" value="application/xml"/> <http:header name="Server" value="Jetty(6.1.26)"/> <http:body media-type="application/xml"/></http:response><html> <div>Section 1</div> <div>Section 2</div> <div>Section 3</div></html></pre> =Errors= {| class="wikitable" width="100%"! width="110"|Code|Description|-|{{Code|HC0001}}|An HTTP error occurred.|-|{{Code|HC0002}}|Error parsing the entity content as XML or HTML.|-|{{Code|HC0003}}|With a multipart response, the override-media-type must be either a multipart media type or application/octet-stream.|-|{{Code|HC0004}}|The src attribute on the body element is mutually exclusive with all other attribute (except the media-type).|-|{{Code|HC0005}}|The request element is not valid.|-|{{Code|HC0006}}|A timeout occurred waiting for the response.|} =Changelog= ;Version 9.0* Updated: support for gzipped content encoding ;Version 8.0* Added: digest authentication ;Version 7.6* Updated: [[#http:send-request|http:send-request]]: {{Code|HC0002}} is raised if the input cannot be parsed or converted to the final data type.* Updated: errors are using {{Code|text/plain}} as media-type.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu