Skip to content

Commit

Permalink
Merge pull request #3419 from dfinity/rei/issue-3417
Browse files Browse the repository at this point in the history
fix issue #3417
  • Loading branch information
jessiemongeon1 authored Sep 4, 2024
2 parents 7f8522f + c6cad62 commit 28f3412
Showing 1 changed file with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@ import { MarkdownChipRow } from "/src/components/Chip/MarkdownChipRow";

## Overview

Canisters can serve or handle an incoming HTTP request using the [HTTP Gateway Protocol](/docs/current/references/http-gateway-protocol-spec).
Canisters can serve or handle an incoming HTTP request using the
[HTTP Gateway Protocol](/docs/current/references/http-gateway-protocol-spec).

This allows developers to host web applications and APIs from a canister.

## How it works

An HTTP request from a client gets intercepted by the HTTP Gateway Protocol, which identifies the target canister and encodes the request in Candid. This encoded request is then sent to the canister for processing. Once processed, the canister replies with an HTTP response. Finally, the HTTP Gateway Protocol decodes the response using Candid and sends it back to the client, completing the communication loop.
An HTTP request from a client gets intercepted by the HTTP Gateway Protocol,
which identifies the target canister and encodes the request in Candid. This
encoded request is then sent to the canister for processing. Once processed, the
canister replies with an HTTP response. Finally, the HTTP Gateway Protocol
decodes the response using Candid and sends it back to the client, completing
the communication loop.

For detailed information on how it works, please refer to the [HTTP Gateway Protocol specification](/docs/current/references/http-gateway-protocol-spec).
For detailed information on how it works, please refer to the
[HTTP Gateway Protocol specification](/docs/current/references/http-gateway-protocol-spec).

## How to make a request

The following example returns 'Hello, World!' in the body at the `/hello` endpoint.
The following example returns 'Hello, World!' in the body at the `/hello`
endpoint.

<AdornedTabs groupId="language">
<TabItem value="motoko" label="Motoko">
Expand Down Expand Up @@ -222,11 +230,18 @@ def http_request(req: HttpRequest) -> HttpResponse:
"body": b"404 Not found :",
}
```
To learn more about serving an HTTP request in Python, refer to [the Kybra book reference on incoming HTTP requests](https://demergent-labs.github.io/kybra/http.html).

To learn more about serving an HTTP request in Python, refer to
[the Kybra book reference on incoming HTTP requests](https://demergent-labs.github.io/kybra/http.html).

</AdornedTab>
</AdornedTabs>

## Additional examples

The [HTTP counter project](../../../references/samples/motoko/http_counter) is an example in Motoko of a 'counter' application that uses the `http_request` method to read the current counter value or access some pre-stored data and the `http_request_update` method to increment the counter and retrieve the updated value.
The
[HTTP counter project](https://github.com/dfinity/examples/tree/master/motoko/http_counter)
is an example in Motoko of a 'counter' application that uses the `http_request`
method to read the current counter value or access some pre-stored data and the
`http_request_update` method to increment the counter and retrieve the updated
value.

0 comments on commit 28f3412

Please sign in to comment.