Changes

Jump to navigation Jump to search
420 bytes removed ,  15:14, 12 September 2011
no edit summary
==Assigning Variables==
====GET Requests====
Query parameters prefixed with a dollar sign (<code>$</code>) will be handled as external variables:
:http://admin:admin@localhost:8984/rest?query=$x*$y&$x=21&$y=2
====POST Requests====
Using POST, the <code>&lt;variable/&gt;></code> element is used to bind external variables to a query:
</query>
</pre>
 
==User Management==
 
By default, the HTTP server starts with no pre-defined user. Users and passwords can be sent via [http://en.wikipedia.org/wiki/Basic_access_authentication HTTP basic access authentication] with each HTTP request. As an alternative, users and passwords can also be specified as command-line arguments or via the "user.basex.user" and "user.basex.password" system properties before the HTTP server is started.
 
With some browsers and with cURL, you can send specify the user name and password with each HTTP request within the request string as plain text, using the format <code>USER:PASSWORD@URL</code>. An example:
 
;GET
:<code>curl -i "bob:alice@localhost:8984/rest/factbook"</code>
==Response Media Type==
===Java===
Most programming languages offer libraries to communicate with HTTP servers.
The following example demonstrates how easy it is to perform a DELETE request with Java. Basic access authentication can be activated in Java by adding an authorization headerto the <code>HttpURLConnection</code> instance. The header contains the word<code>Basic</code>, which specifies the authentication method, followed by theBase64-encoded <code>USER: PASSWORD</code> pair. As Java does not include a defaultconversion library for Base64 data, the internal BaseX class<code>org.basex.util.Base64</code> can be used for that purpose: 
<pre class="brush:java">
// Set as DELETE request.
conn.setRequestMethod("DELETE");
 
// User and password.
String user = "bob";
String pw ="alice";
// Encode user name and password pair with a base64 implementation.
String encoded = Base64.encode(user + ":" + pw);
// Basic access authentication header to connection request.
conn.setRequestProperty("Authorization", "Basic "+encoded);
// Print the HTTP response code.
:<code>curl -i -X DELETE "admin:admin@localhost:8984/rest/factbook"</code>
==User Management==
 
By default, the HTTP server starts with no pre-defined user. Users and passwords can be sent via [http://en.wikipedia.org/wiki/Basic_access_authentication HTTP basic access authentication] with each HTTP request. As an alternative, users and passwords can also be specified as command-line arguments or via the "user.basex.user" and "user.basex.password" system properties before the HTTP server is started.
 
With some browsers and with cURL, you can send specify the user name and password with each HTTP request within the request string as plain text, using the format <code>USER:PASSWORD@URL</code>. An example:
 
;GET
:<code>curl -i "bob:alice@localhost:8984/rest/factbook"</code>
 
===Java Example===
 
Basic access authentication can be activated in Java by adding an authorization header to your <code>HttpURLConnection</code> instance. The header contains the word <code>Basic</code>, which specifies the authentication method, followed by the base64-encoded <code>USER:PASSWORD</code> pair. The BaseX internal <code>org.basex.util.Base64</code> can be used for encoding strings to the Base64 format:
 
<pre class="brush:java">
// The java URL connection to the resource.
URL url = new URL("http://localhost:8984/rest/factbook");
// Establish the connection to the URL.
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// Set as GET request.
conn.setRequestMethod("GET");
// User and password.
String user = "bob";
String pw ="alice";
// Encode user name and password pair with a base64 implementation.
String encoded = Base64.encode(user + ":" + pw);
// Basic access authentication header to connection request.
conn.setRequestProperty("Authorization", "Basic "+encoded);
// Print the HTTP response code.
System.out.println("\n* HTTP response: " + conn.getResponseCode());
// Close connection.
conn.disconnect();
</pre>
 
[[Category:Server]]
[[Category:REST]]
[[Category:Developer]]
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu