getting request's headers inside a Web Application
Dear all,
I do not see an option to obtain the headers from the HTTP request from inside webapp function. Just like in the example:
declare %rest:GET %rest:path('tests-html') ???%rest:headers('User-Agent')??? %output:method('html') function page-main:start-empty( ???$user-agent as xs:string??? ) as element(html) { ... }
Is this possible?
BR
Hi BR,
When called from within a WebApp this should give the result you are looking for..
*request:header*( 'User-Agent')
See https://docs.basex.org/wiki/Request_Module#request:header
/Andy
Quoted text · dudzikp at yahoo.com · 22 lines
On Tue, 15 Jun 2021 at 15:33, dudzikp at yahoo.com <dudzikp at yahoo.com> wrote: > Dear all, > > I do not see an option to obtain the headers from the HTTP request from > inside webapp function. Just like in the example: > > declare > %rest:GET > %rest:path('tests-html') > ???%rest:headers('User-Agent')??? > %output:method('html') > function page-main:start-empty( > ???$user-agent as xs:string??? > ) as element(html) { > ... > } > > Is this possible? > > > BR >
Hi BR,
You can also use the %rest:header-param annotation:
declare
%rest:path('/test')
%rest:header-param('User-Agent', '{$agent}')
function local:test($agent) {
element agent { $agent }
};Check out [1] for "User-Agent" and additional examples.
Best, Christian
Quoted text · dudzikp at yahoo.com · 21 lines
On Tue, Jun 15, 2021 at 4:33 PM dudzikp at yahoo.com <dudzikp at yahoo.com> wrote: > > Dear all, > > I do not see an option to obtain the headers from the HTTP request from inside webapp function. Just like in the example: > > declare > %rest:GET > %rest:path('tests-html') > ???%rest:headers('User-Agent')??? > %output:method('html') > function page-main:start-empty( > ???$user-agent as xs:string??? > ) as element(html) { > ... > } > > Is this possible? > > > BR
Thanks guys! I have no idea how I missed that.
BR
Quoted text · Christian Grün · 1 line
On Tuesday, June 15, 2021, 6:21:34 PM GMT+2, Christian Grün <christian.gruen at gmail.com> wrote:
Hi BR,
You can also use the %rest:header-param annotation:
declare
%rest:path('/test')
%rest:header-param('User-Agent', '{$agent}')
function local:test($agent) {
element agent { $agent }
};Check out [1] for "User-Agent" and additional examples.
Best, Christian
Quoted text · dudzikp at yahoo.com · 21 lines
On Tue, Jun 15, 2021 at 4:33 PM dudzikp at yahoo.com <dudzikp at yahoo.com> wrote: > > Dear all, > > I do not see an option to obtain the headers from the HTTP request from inside webapp function. Just like in the example: > > declare > %rest:GET > %rest:path('tests-html') > ???%rest:headers('User-Agent')??? > %output:method('html') > function page-main:start-empty( > ???$user-agent as xs:string??? > ) as element(html) { > ... > } > > Is this possible? > > > BR
Written for BaseX 9.5. BaseX may have changed since: please check the current documentation.