Changes

Jump to navigation Jump to search
573 bytes added ,  19:58, 16 February 2018
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. With {{Version|9.0}}, 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|exerr}} prefix.
=Functions=
|-
| '''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 of {{Code|http:request}} element follows the [http://expath.org/spec/http-client EXPath] specification. Both basic and digest authentication is supported.
|-
|'''Errors'''
|{{Error|HC0001|#Errors}} an HTTP error occurred.<br/>{{Error|HC0002|#Errors}} error parsing the entity content as XML or HTML.<br/>{{Error|HC0003|#Errors}} with a multipart response, the override-media-type must be either a multipart media type or application/octet-stream.<br/>{{Error|HC0004|#Errors}} the src attribute on the body element is mutually exclusive with all other attribute (except the media-type).<br/>{{Error|HC0005|#Errors}} the request element is not valid.<br/>{{Error|HC0006|#Errors}} a timeout occurred waiting for the response.
|-
| '''Notes'''
|The attribute {{Code|auth-method}} of {{Code|$request}} is not considered in our implementation because we are handling only basic authentication.
|}
==Examples==
===Status Only===
Simple GET request. As the attribute {{Code|status-only}} is set to true, only the response element is returned.
</http:response></pre>
===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 class="brush:xquery">http:send-request(<http:request method='get' href='http://www.google.com' timeout='10'/>)</pre>
'''Result:'''
<pre class="brush:xml">
</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}}). The result With the {{Code|http:header}} element, a custom user agent can then be further processedset. See the following example:
'''Query:'''
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 {
===SVG Data===
 
Content-type ending with +xml, e.g. image/svg+xml.
</svg></pre>
===POST Request=== 
POST request to the BaseX REST Service, specifying a username and password.
method='post' username='admin' password='admin' send-authorization='true'>
<http:body media-type='application/xml'>
<query xmlns="http://basex.org/rest"> <text><![CDATA[ <html>{ for $i in 1 to 3 return <div>Section {$i }</div> }</html> ]]></text> </query>
</http:body>
</http:request>
=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]]: {{ErrorCode|HC0002|#Errors}} 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