XQuery/Basic Authentication

< XQuery


You want to use a very basic login process over a secure network such as a secure Intranet or over an SSL connection.


We will used the base64 encoding and decoding tools to generate the right strings.

xquery version "1.0";

let $user := 'Aladdin'
let $password := 'open sesame'
let $credentials := concat($user, ':', $password)
let $encode := util:string-to-binary($credentials)

Returns the following:

   <password>open sesame</password>

Sample HTTP GET using Basic AuthenticationEdit

xquery version "1.0";

declare function local:basic-get-http($uri,$username,$password) {
  let $credentials := concat($username,":",$password)
  let $credentials := util:string-to-binary($credentials)
  let $headers  := 
      <header name="Authorization" value="Basic {$credentials}"/>
  return httpclient:get(xs:anyURI($uri),false(), $headers)

let $host := "http://localhost:8080"
let $path :=  "/exist/rest/db/apps/terms/data/1.xml"
let $uri := concat($host, $path)
let $user := 'my-login'
let $password := 'my-password'
return local:basic-get-http($uri,$username,$password)