Requests

Requests

When handling requests, several pieces of data from the incoming request is made available via the context argument that is provided to render and callbacks.

Headers

The functions context.request.has_header() and context.request.get_header() can be used to check or fetch headers from the incoming request. The header name is lowercased during receiving, so these functions are not case sensitive on the header name.

global.server.add_path("demos/request-headers", function(_context) {
	if (!_context.request.has_header("authorization")) {
		_context.response.send_string("Not authorized", 401);
	}
	_context.response.send_string("OK");
});

In the above example, the Authorization header is checked, and if there isn't one, a message is returned indicating a problem. If there was one, an OK message is returend

Path and Query parameters

The function context.request.get_parameter() can be used to fetch path parameters, and the function context.request.get_parameter() can be used to fetch query parameters from the incoming request.

global.server.add_path("demos/{name}", function(_context) {
	var _name = _context.request.get_parameter("name");
	var _id = _context.request.get_query("id");
	_context.response.send_string($"hello {_name}, your id is {_id}");
});

In the above example, if the user requests the path demos/steve?id=123, then this would result in the value steve bein available as a parameter, and 123 being available as a query parameter.

Method

The HTTP method used can be accessed using context.rquest.get_method() it will have values like "GET", "POST", "DELET", and so on.

global.server.add_path("demos/method", function(_context) {
	if (_context.request.method == "GET") {
		_context.response.send_string("a get request");
	}
	else if (_context.request.method == "POST") {
		_context.response.send_string("a post request");
	}
});

Body data

The raw body data can be accessed using the property context.request.data, however it is more common to read this as a string or a json, using context.request.get_data_as_string() and context.request.get_data_as_json().

global.server.add_path("demos/body", function(_context) {
	if (_context.request.get_header("content-type") == "application/json") {
		var _json = _context.request.get_data_as_json();
		_context.response.send_string($"payload: {_json.something}");
	}
	else {
		var _text = _context.request.get_data_as_string();
		_context.response.send_string($"payload: {_text}");
	}
});

Form parameters

If the incoming request contains form parameters, they can be accessed using context.request.get_form("name"). This can be used to fetch data from HTML forms.

global.server.add_path("demos/form", function(_context) {
	_context.response.send_html(@'
		<form action="/demos/submit" method="post">
			<label for="name">Name: </label>
			<input type="text" id="name" name="name">
			<br>
			<label for="name">Fruit: </label>
			<input type="text" id="fruit" name="fruit">
			<br>
			<input type="submit" value="Submit">
		</form>
	');
});

global.server.add_path("demos/submit", function(_context) {
	var _name = _context.request.get_form("name");
	var _name = _context.request.get_form("fruit");
	_context.response.send_string($"hello {_name} who likes {_fruit}");
});

In the above example, if a form is submitted to that page with the field name name, the value will be accessed. You can see the demo here.

File uploads

Upload files can be accessed using context.request.get_file_buffer("file") and context.request.get_file_name("file") to get the buffer and the filename respectively. The buffers will be cleaned up once the endpoint responds.

global.server.add_path("demos/file", function(_context) {
	_context.response.send_html(@'
		<form action="/demos/file-submit" method="post">
			<label for="name">Name: </label>
			<input type="text" id="name" name="name">
			<br>
			<input type="file" name="file">
			<br>
			<input type="submit" value="Submit">
		</form>
	');
});

global.server.add_path("demos/file-submit", function(_context) {
	var _name = _context.request.get_form("name");
	var _file = _context.request.get_file("file");
	_context.response.send_string($"hello {_name} you uploaded filesize: {_file.size}");
});

In the above example, a file upload form submits a file, and the buffer length is printed out. You can see the demo here.