API Reference

In the live editor where you implement your API, you are required to implement and return a javascript function.

This function will be passed a transaction object, where you may retrieve details about the request, and pass contents of the response. When you're done dealing with the request, you must call the ready() function of the transaction object, in order for a response to be returned to the client.

/*
 * Function to handle requests and prepare responses
 */
var process = function(transaction) {
	return transaction.setJSON({}).ready();
}
return process;

Generic information

The request object is available as transaction.req.

transaction.url
The base URL of the server, such as http://379.httpjs.net/.
transaction.hide()
Do not show this transaction in the output above. In example use this for favicons and OPTIONS requests, if they clutter your debugging info.

The request

The request object is available as transaction.req.

transaction.req.headers
A indexed object containing the headers in the request.
transaction.req.path
The path of the request, including the query string if present
transaction.req.url
The path of the request, including the query string if present
transaction.req.ip
The IP address of the requesting client.
transaction.req.method
The HTTP method of the request, such as GET, PUT or POST.
transaction.req.query
An object representing the query string as parameters.
transaction.req.body
The parsed body of the request. If no body was included in the request, this property will be an empty object. It is a parsed object, both if the real request body was encoded in JSON or as form-encoded.
transaction.req.rawBody
To be supported later. Stay tuned.
t.req.headers

Preparing the response

Most importantly remember to always call the transaction.ready() when completed.

transaction.setHeader(key, val)
Set HTTP response header
transaction.setBody(text, type)
Set HTTP response body as a string. Optionally provide the content type as a second parameter.
transaction.setJSON(obj)
Set HTTP response by passing a JSON object. The Content-type, and JSON serialization will be handled automatically.
transaction.setStatus(code)
Set HTTP status code in the response..
transaction.redirect(url)
Redirect to a new URL. Will return 302 with HTML content.
transaction.error(msg)
Returns 500 Internal error witha HTML output with the included message.
transaction.notFound()
Returns 404 Not found.

UNINETT ♥ HTTP

We hope you find this tool useful. For comments contact Andreas Åkre Solberg or ask questions, request features or report issues.