HTTP counter
View this sample's code on GitHub
Overview
The example demonstrates a counter dapp and an HTTP interface. It is essentially an iteration on the counter canister which adds native HTTP interfaces.
This sample dapp provides an interface that exposes the following methods:
http_request
, which can:GET
some staticgzip
ed data ifgzip
is accepted.GET
the counter otherwise.- Refer
POST
s to callhttp_request_update
. - Returns
400
for all other requests.
http_request_update
, which can:POST
to increment the counter.- returning some static
gzip
ed data ifgzip
is accepted. - otherwise return the new counter value.
- returning some static
- Returns
400
for all other requests.
Prerequisites
This example requires an installation of:
Install the BIG SDK.
Clone the example dapp project:
git clone https://github.com/dfinity/examples
Begin by opening a terminal window.
Step 1: Navigate into the folder containing the project's files and start a local instance of the BigFile with the command:
cd examples/motoko/http_counter
dfx start --background
Step 2: Deploy the canister:
dfx deploy
Step 3: Take note of the canister ID to form URLs at which the http_counter
is accessible.
CANISTER_ID=$(dfx canister id http_counter)
echo "http://localhost:4943/?canisterId=$CANISTER_ID"
echo "http://$CANISTER_ID.localhost:4943/"
Step 4: All functionality of the canister can be exercised with the following commands:
CANISTER_ID=$(dfx canister id http_counter)
# Get the counter
curl "$CANISTER_ID.localhost:4943/" --resolve "$CANISTER_ID.localhost:4943:127.0.0.1"
# Get the static gzipped query content
curl --compressed "$CANISTER_ID.localhost:4943/" --resolve "$CANISTER_ID.localhost:4943:127.0.0.1"
# Increment the counter
curl -X POST "$CANISTER_ID.localhost:4943/" --resolve "$CANISTER_ID.localhost:4943:127.0.0.1"
# Increment the counter and get the static gzipped update content
curl --compressed -X POST "$CANISTER_ID.localhost:4943/" --resolve "$CANISTER_ID.localhost:4943:127.0.0.1"
Security considerations and best practices
If you base your application on this example, we recommend you familiarize yourself with and adhere to the security best practices for developing on the BigFile. This example may not implement all the best practices.
For example, the following aspect is particularly relevant for this app:
- Use HTTP asset certification and avoid serving your dApp through raw.ic0.app, in case the HTTP responses should come with authenticity guarantees.