Http Request

Alpas wraps every request that hits your app with an HttpCall object. HttpCall is no doubt the most powerful object in Alpas. It has everything you need to get any data and operations you need out of a request.

Retrieving Request Properties

  • method

Returns you an enum of type dev.alpas.http.Method telling you the HTTP verb used by the request.

  • url

Returns you the URL for the incoming request *without- the query string.

  • fullUrl

Gives you the URL for the incoming request *including- the query string.

  • isAjax

Tells you whether the request is an ajax request or not.

  • isPjax

Tells you whether the request is a Pjax request or not.

  • isJson

Tells you whether the request content is of type 'application/json' or not.

  • wantsJson

Tells you whether the request wants a JSON response or not.

  • expectsJson

Tells you whether the request is "expecting" a JSON response or not. This is true if the request has explicitly mentioned that it wants a JSON response or if the request is an AJAX request.

  • body

Gives you the body of the request as a UTF-8 encoded string.

  • jsonBody

Deserializes the body of the request and returns as a HashMap<String, Any>.

Retrieving Request Parameters

  • params : Map<String, List<Any>?>?

Gives you the combined values of all the request parameters—query params, form params, and route params.

  • routeParams : Map<String, List<Any>?>?

Gives you the values of just the route parameters.

  • queryParams : Map<String, List<Any>?>?

Gives you the values of just the query parameters.

  • fun params(key: String) : List<Any>?

Returns all the values for the given key as a nullable list.

  • fun params(key: String, vararg keys: String): Map<String, Any?>

Returns a map of values for only the specified keys.

  • fun paramsExcept(key: String, vararg keys: String): Map<String, Any?>

Returns a map of values for everything but the specified keys.


//...

val only = call.params("username", "email", "phone")
val except = call.paramsExcept("password", "password_confirm")

//...

  • fun routeParams(key: String) : List<Any>?

Returns all the route parameter values for the given key.

  • fun queryParams(key: String) : List<Any>?

Returns all the query parameter values for the given key.

  • fun param(key: String) : Any?

Returns the value of the given key if it exists otherwise returns null.

  • fun routeParam(key: String) : Any?

Returns the route parameter value of the given key.

  • fun queryParam(key: String) : Any?

Returns the query parameter value of the given key.

  • fun stringParam(key: String) : String?

Returns the value of the given key as a nullable string.

  • fun intParam(key: String) : Int?

Returns the value of the given key as a nullable int.

  • fun longParam(key: String) : Long?

Returns the value of the given key as a nullable long.

  • fun boolParam(key: String) : Boolean?

Returns the value of the given key as a nullable bool.

/alert/The params(key: String, vararg keys: String) returns only the values for the keys that actually exist in the request.

/info/Alpas *doesn't- merge the JSON body of a request with other parameters and doesn't make them available through one of the above paramX() methods. Instead, you need to get the jsonBody property and then extract the param you want yourself.

Retrieving Headers

  • fun header(key: String) ? String?

Returns the header value for a given key if exists. If multiple values exist for the given key, it returns the first value.

  • fun headers(key: String) ? List<String>?

Returns all the header values for the given key if exists.

Retrieving Cookies

To read the cookies from an incoming request, use the cookie property of the HttpCall object.


//...

call.cookie.get("username")
// Alternatively
call.cookie("username")
// Alternatively
call.cookie["username"]

//...

/info/ Alpas encrypts and signs almost all of your outgoing cookies and decrypts them when it receives a request. If the cookies are changed by the client, they will be invalidated and removed automatically as well.

Asynchronous Request

Alpas supports asynchronous request processing by passing a CompletableFuture instance to the HttpCall#hold() method.


// ...

fun store(call: HttpCall) {
    call.hold(delay(1000))
}

private val executorService: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor()
private fun delay(sleep: Int) : CompletableFuture<String> {
    return CompletableFuture<String>().apply {
        executorService.schedule({ complete("ok") }, sleep, TimeUnit.MILLISECONDS)
    }
}

Your completable future must be either of type String or an object of a class implementing dev.alpas.http.Response interface. So, if you want to render a template asynchronously, you have to return an instance of ViewResponse, which already implements the Response interface. Similarly, there is JsonResponse class for returning back something to the client as a JSON object.


// ...

fun store(call: HttpCall) {
    call.hold(delay(1000))
}

private val executorService: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor()
private fun delay(sleep: Int) : CompletableFuture<ViewResponse> {
    return CompletableFuture<ViewResponse>().apply {
        executorService.schedule({ complete(ViewResponse("welcome")) }, sleep, TimeUnit.MILLISECONDS)
    }
}