Changes

Jump to navigation Jump to search
45 bytes removed ,  18:38, 1 December 2023
m
Text replacement - "syntaxhighlight" to "pre"
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.
Please note that BaseX provides extensions to the specification: * Since {{Announce|Updated with Version 10:11}}, {{Code|csv}}, {{Code|json}} and {{Code|html}} The Implementation parser options can be supplied to influence the conversion of the response. Since BaseX 10, the module is now based on the new [https://openjdk.org/groups/net/httpclient/intro.html Java HTTP Client], which provides a better overall performance, uses internal connection pools and follows redirects across different protocols (http, https).
=Conventions=
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>http:send-request(<http:request method='get' status-only='true'/>, 'http://basex.org')</syntaxhighlightpre>
'''Result:'''
<syntaxhighlight pre lang="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></syntaxhighlightpre>
==Google Homepage==
Retrieve the Google search home page with a timeout of 10 seconds. In order to [[Parsers#HTML_ParserHTMLParser|parse HTML]], TagSoup must be contained in the class path.
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>http:send-request(<http:request method='get' href='http://www.google.com' timeout='10'/>)</syntaxhighlightpre>
'''Result:'''
<syntaxhighlight pre lang="xml">
<http:response status="200" message="OK">
<http:header name="Date" value="Mon, 14 Mar 2011 22:03:25 GMT"/>
</body>
</html>
</syntaxhighlightpre>
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:'''
<syntaxhighlight pre lang="'xquery"'>
let $binary := http:send-request(
<http:request method='get'
'Conversion to XML failed: ' || $err:description
}
</syntaxhighlightpre>
===SVG Data===
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>http:send-request(<http:request method='get'/>, 'http://upload.wikimedia.org/wikipedia/commons/6/6b/Bitmap_VS_SVG.svg')</syntaxhighlightpre>
'''Result:'''
<syntaxhighlight pre lang="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></syntaxhighlightpre>
==POST Request==
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>
http:send-request(
<http:request method='post' username='admin' password='admin'>
</query>
)
</syntaxhighlightpre>
'''Result:'''
<syntaxhighlight pre lang="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>
</syntaxhighlightpre>
==File Upload==
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>
let $path := 'file-to-be.uploaded'
return http:send-request(
file:read-binary($path)
)
</syntaxhighlightpre>
'''RESTXQ service:'''
<syntaxhighlight pre lang="'xquery"'>
declare
%rest:POST
});
};
</syntaxhighlightpre>
==Response Conversion==
Converts the response body CSV, JSON and HTML responses are automatically converted to a specific JSON an XML representation. The target formatcan be influenced by supplying {{Code|csv}}, {{Code|json}} and {{Code|html}} attributes:
'''Query:'''
<syntaxhighlight pre lang="'xquery"'>http:send-request(<http:request method='GET' href='http://localhost:8080/json' json='format=xquery,lax=true'/>)</syntaxhighlightpre>
'''Result:'''
<syntaxhighlight pre lang="javascript">
map { "abcde": 12345 }
</syntaxhighlightpre>
Without the {{Code|csvjson}} attribute, the response body is converted to the default XML representation:
<syntaxhighlight pre lang="xml">
<json type="object">
<abcde>12345</abcde>
</json>
</syntaxhighlightpre>
'''RESTXQ service:'''
<syntaxhighlight pre lang="'xquery"'>
declare
%rest:path('json')
map { 'abcde': 12345 }
};
</syntaxhighlightpre>
See the [[CSV Module]], [[JSON Module]] and [[HTML Module]] for a list of the available options.
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu