Changes

Jump to navigation Jump to search
910 bytes added ,  23:42, 1 February 2020
All functions in this module are assigned to the <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><nowiki>http://expath.org/ns/error</nowiki></code> namespace, which is statically bound to the {{Code|exerrexperr}} prefix.
=Functions=
* If the URI is not given with the parameter {{Code|$href}}, its value in {{Code|$request}} is used instead.
* The request body can also be supplied via the {{Code|$bodies}} parameter.
* Certificate verification can be globally disabled via the {{Option|IGNORECERT}} option.
Notes:
'''Query:'''
<pre class="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)
</pre>
<div>Section 3</div>
</html>
</pre>
 
==File Upload==
 
Performs an HTML file upload. In the RESTXQ code, the uploaded file is written to the temporary directory:
 
'''Query:'''
 
<pre class="brush: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)
)
</pre>
 
'''RESTXQ service:'''
 
<pre class="brush: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)
});
};
</pre>
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu