Changes

Jump to navigation Jump to search
1,585 bytes added ,  16:30, 27 February 2020
no edit summary
This [[Module Library|XQuery Module]] contains a single function to send HTTP requests and handle HTTP responses. The function {{Code|send-request}} is based on the [http://expath.org/spec/http-client EXPath HTTP Client Module]. It gives full control over the available request and response parameters. For simple GET requests, the [[Fetch Module]] may be sufficient.
 
If <code><http:header name="Accept-Encoding" value="gzip"/></code> is specified and if the addressed web server provides support for the {{Code|gzip}} compression algorithm, the response will automatically be decompressed.
=Conventions=
All functions in this module are assigned to the {{Code|<code><nowiki>http://expath.org/ns/http-client}} </nowiki></code> namespace, which is statically bound to the {{Code|http}} prefix.<br/>All errors are assigned to the {{Code|<code><nowiki>http://expath.org/ns/error}} </nowiki></code> namespace, which is statically bound to the {{Code|exerrexperr}} prefix.
=Functions=
|-
| width='120' | '''Signatures'''
|{{Func|http:send-request|$request as element(http:request)?, $href as xs:string?, $bodies as item()*|item()+}}<br/>{{Func|http:send-request|$request as element(http:request)?, $href as xs:string?|item()+}}<br />{{Func|http:send-request|$request as element(http:request)?, $href as xs:string?, $bodies as item()*|item()+}}<br />
|-
| '''Summary'''
|Sends an HTTP request and interprets the corresponding response. :* {{Code|$request}} 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|$href}}, its value in {{Code|$request}} is used instead.<br/>* The structure request body can also be supplied via the {{Code|$bodies}} parameter.* Certificate verification can be globally disabled via the {{Option|IGNORECERT}} option. Notes:* Both basic and digest authentication is supported.* While the contents of the request can be supplied as child of the {{Code|http:requestbody}} element follows , it is faster and safer to pass them on via the third argument.* For further information, please check out the [http://expath.org/spec/http-client EXPath] specification. Since {{Version|8.0}}, digest authentication is supported as well.
|-
|'''Errors'''
|}
==Examples==
===Status Only===
Simple GET request. As the attribute {{Code|status-only}} is set to true, only the response element is returned.
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">http:send-request(<http:request method='get' status-only='true'/>, 'http://basex.org')</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml"><http:response status="200" message="OK">
<http:header name="Date" value="Mon, 14 Mar 2011 20:55:53 GMT"/>
<http:header name="Content-Length" value="12671"/>
<http:header name="Cache-Control" value="max-age=90"/>
<http:body media-type="text/html; charset=utf-8"/>
</http:response></presyntaxhighlight==Google Homepage==
===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 contained in the class path in order to parse html.
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">http:send-request(<http:request method='get' href='http://www.google.com' timeout='10'/>)</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
<http:response status="200" message="OK">
<http:header name="Date" value="Mon, 14 Mar 2011 22:03:25 GMT"/>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="contentContent-typeType" content="text/html; charset=ISO-8859UTF-18"/>
<title>Google</title>
<script>window.google={kEI:"rZB- ... </script> </center>
</body>
</html>
</presyntaxhighlight>
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 classsyntaxhighlight lang="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"/>
'Conversion to XML failed: ' || $err:description
}
</presyntaxhighlight>
===SVG Data===
 
Content-type ending with +xml, e.g. image/svg+xml.
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">http:send-request(<http:request method='get'/>, 'http://upload.wikimedia.org/wikipedia/commons/6/6b/Bitmap_VS_SVG.svg')</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml"><http:response status="200" message="OK">
<http:header name="ETag" value="W/&quot;11b6d-4ba15ed4&quot;"/>
<http:header name="Age" value="9260"/>
</linearGradient>
...
</svg></presyntaxhighlight==POST Request==
===POST Request===
POST request to the BaseX REST Service, specifying a username and password.
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">let $http:send-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:request>, 'http://basex.orglocalhost:8984/rest"', <query> <text><![CDATA[ <html>{ for $i in 1 to 3 return <div>Section {$i }</div> }</html> ]]> </text> </query> </http:body> </http:request>return http:send-request($request)</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
<http:response xmlns:http="http://expath.org/ns/http-client" status="200" message="OK">
<http:header name="Content-Length" value="135"/>
<div>Section 3</div>
</html>
</presyntaxhighlight> ==File Upload== Performs an HTML file upload. In the RESTXQ code, the uploaded file is written to the temporary directory: '''Query:''' <syntaxhighlight lang="xquery">let $path := 'file-to-be.uploaded'return http:send-request( <http:request method='POST'> <http:multipart media-type='multipart/form-data'> <http:header name='content-disposition' value='form-data; name="files"; filename="{ file:name($path) }"'/> <http:body media-type='application/octet-stream'/> </http:multipart> </http:request>, 'http://localhost:8984/write-to-temp', file:read-binary($path))</syntaxhighlight> '''RESTXQ service:''' <syntaxhighlight lang="xquery">declare %rest:POST %rest:path('/write-to-temp') %rest:form-param('files', '{$files}')function dba:file-upload( $files as map(xs:string, xs:base64Binary)) as empty-sequence() { map:for-each($files, function($file, $content) { file:write-binary(file:temp-dir() || $file, $content) });};</syntaxhighlight>
=Errors=
=Changelog=
 
;Version 9.0
* Updated: support for gzipped content encoding
;Version 8.0
* 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.
 
[[Category:XQuery]]
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu