Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/send_http_post.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: send_http_post

on:
push:
branches: [master]
pull_request:
paths:
- motoko/send_http_post/**
- .github/workflows/send_http_post.yml

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
motoko-send_http_post:
runs-on: ubuntu-24.04
container: ghcr.io/dfinity/icp-dev-env-motoko:0.3.2
env:
ICP_CLI_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
- name: Deploy and test
working-directory: motoko/send_http_post
run: |
icp network start -d
icp deploy
make test
20 changes: 0 additions & 20 deletions motoko/send_http_post/.devcontainer/devcontainer.json

This file was deleted.

6 changes: 3 additions & 3 deletions motoko/send_http_post/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
# dfx temporary files
.dfx/

# icp-cli cache
.icp/

# generated files
src/declarations/

# frontend code
node_modules/
dist/

# environment variables
.env
113 changes: 0 additions & 113 deletions motoko/send_http_post/BUILD.md

This file was deleted.

27 changes: 6 additions & 21 deletions motoko/send_http_post/Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
.PHONY: all
all: build

.PHONY: node_modules
.SILENT: node_modules
node_modules:
npm install

.PHONY: deploy
.SILENT: deploy
build: node_modules
dfx canister deploy send_http_post_backend

.PHONY: test
.SILENT: test
test: deploy
dfx canister call send_http_post_backend send_http_post_request \
| grep 'POST request from an ICP canister' && echo 'PASS'

.PHONY: clean
.SILENT: clean
clean:
rm -fr .dfx
test:
@echo "=== Test 1/1: send_http_post_request returns POST echo ==="
@result=$$(icp canister call backend send_http_post_request '()') && \
echo "$$result" && \
echo "$$result" | grep -q 'POST request from an ICP canister' && \
echo "PASS" || (echo "FAIL" && exit 1)
34 changes: 17 additions & 17 deletions motoko/send_http_post/README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# HTTP: POST

The purpose of this dapp is to give developers a minimal dapp that uses the IC's HTTPS outcalls feature to make a `POST` request.
This example demonstrates how to use the ICP HTTPS outcalls feature to make a `POST` request from a Motoko canister. It sends a plain-text body to `postman-echo.com/post`, which echoes back the request as JSON, allowing you to verify the POST body and headers were sent correctly.

This demo goes in hand with the [developer documentation on HTTPS outcalls](https://internetcomputer.org/docs/building-apps/network-features/using-http/https-outcalls/post).
## Build and deploy from the command line

## Deploying from ICP Ninja

[![](https://icp.ninja/assets/open.svg)](https://icp.ninja/editor?g=https://github.com/dfinity/examples/tree/master/motoko/send_http_post)

## Build and deploy from the command-line

### 1. [Download and install the IC SDK.](https://internetcomputer.org/docs/building-apps/getting-started/install)

### 2. Download your project from ICP Ninja using the 'Download files' button on the upper left corner, or [clone the GitHub examples repository.](https://github.com/dfinity/examples/)

### 3. Navigate into the project's directory.

### 4. Deploy the project to your local environment:
### Prerequisites
- Node.js
- icp-cli: `npm install -g @icp-sdk/icp-cli @icp-sdk/ic-wasm`

### Install
```bash
git clone https://github.com/dfinity/examples
cd examples/motoko/send_http_post
```
dfx start --background --clean && dfx deploy

### Deploy and test
```bash
icp network start -d
icp deploy
make test
icp network stop
```

## Security considerations and best practices

If you base your application on this example, it is recommended that you familiarize yourself with and adhere to the [security best practices](https://internetcomputer.org/docs/building-apps/security/overview) for developing on ICP. This example may not implement all the best practices.
Refer to the [security best practices](https://docs.internetcomputer.org/guides/security/overview) for information on security and best practices for your ICP app.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Blob "mo:core/Blob";
import Text "mo:core/Text";
import IC "ic:aaaaa-aa";
import { ic } "mo:ic";
import IC "mo:ic/Types";

persistent actor {
persistent actor SendHttpPost {

// #region transform
// Strip HTTP response headers (date, cookies, tracking IDs) that vary across requests.
Expand All @@ -11,17 +12,17 @@ persistent actor {
// essential for consensus to succeed.
public query func transform({
context : Blob;
response : IC.http_request_result;
}) : async IC.http_request_result {
response : IC.HttpRequestResult;
}) : async IC.HttpRequestResult {
{ response with headers = [] };
};
// #endregion transform

// #region post_request
public func send_http_post_request() : async Text {
let body = Text.encodeUtf8("This is a POST request from an ICP canister.");
let body = "This is a POST request from an ICP canister.".encodeUtf8();

let request : IC.http_request_args = {
let request : IC.HttpRequestArgs = {
url = "https://postman-echo.com/post";
// Always set max_response_bytes to a tight bound. The cycle cost scales
// with this value, not the actual response size. If omitted, the system
Expand All @@ -42,11 +43,11 @@ persistent actor {

// Cycles must be explicitly attached to management canister calls.
// The amount is based on request size and max_response_bytes.
let response = await (with cycles = 230_949_972_000) IC.http_request(request);
let response = await (with cycles = 230_949_972_000) ic.http_request(request);

// postman-echo.com echoes back the request data as JSON, letting you
// verify the POST body and headers were sent correctly.
switch (Text.decodeUtf8(response.body)) {
switch (response.body.decodeUtf8()) {
case (?text) text;
case null "Response is not valid UTF-8";
};
Expand Down
15 changes: 0 additions & 15 deletions motoko/send_http_post/dfx.json

This file was deleted.

4 changes: 4 additions & 0 deletions motoko/send_http_post/icp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
canisters:
- name: backend
recipe:
type: "@dfinity/motoko@v5.0.0"
18 changes: 10 additions & 8 deletions motoko/send_http_post/mops.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Motoko dependencies (https://mops.one/)

[toolchain]
moc = "1.5.1"
moc = "1.9.0"

[dependencies]
core = "2.4.0"
core = "2.5.0"
ic = "4.0.0"

[moc]
# M0236: use context dot notation (e.g. map.get(k) instead of Map.get(map, compare, k))
# M0237: redundant explicit implicit arguments (e.g. Nat.compare is inferred automatically)
# M0223: redundant type instantiation (e.g. Array.tabulate instead of Array.tabulate<T>)
args = ["-W=M0236,M0237,M0223"]
# M0236: use context dot notation
# M0237: redundant explicit implicit arguments
# M0223: redundant type instantiation
args = ["--default-persistent-actors", "-W=M0236,M0237,M0223"]

[canisters.backend]
main = "backend/app.mo"
48 changes: 0 additions & 48 deletions motoko/send_http_post/package.json

This file was deleted.

Loading
Loading