How to do an http_request_update in Motoko?

Hey!

I have been trying to use the http_request_update in my motoko canister but without success.

Below are my 2 functions, http_request and http_request_update, but I can never get the http_request_update to be called, my post requests just return me the http_request result…

Is there a special post resquest to do ?
can someone give me a curl command or a js fetch script that calls it ?

Thanks in advance!

import Principal "mo:base/Principal";
import Http "mo:certified-cache/Http";
import Text "mo:base/Text";
import Error "mo:base/Error";
import Debug "mo:base/Debug";
import Nat "mo:base/Nat";
import Frontend "frontend/__html__";
import Scan "scan";

shared ({ caller = creator }) actor class Boot() = this {

    type HttpRequest = Frontend.Request;
    type HttpResponse = Frontend.Response;

    stable var scan_count : Nat = 0;

    var name : Text = "Boot";

    public func http_request_update(request : Http.HttpRequest) : async Http.HttpResponse {
        let counter = Scan.scan(request.url, scan_count);
        if (counter > 0) {
            scan_count := counter;
        };
        return {
            body = Text.encodeUtf8("Scan Count: " # Nat.toText(scan_count));
            headers = [("Content-Type", "text/plain")];
            status_code = 200;
            streaming_strategy = null;
            upgrade = ?true;
        }
    };

    
    public query func http_request(request : Frontend.Request) : async Frontend.Response {


        try {
        let counter = Scan.scan(request.url, scan_count);

        Debug.print("Valid: " # Nat.toText(counter)# " Scan Count: " # Nat.toText(scan_count));
        let new_request = {
            url = if (counter > 0) {
                "/valid.html"
            } else {
                "/invalid.html"
            };
            method = request.method;
            body = request.body;
            headers = request.headers;
        };

        return (Frontend.http_request(new_request));
        } catch (err) {
            return ({
                body = Text.encodeUtf8("Error: " # Error.message(err));
                headers = [("Content-Type", "text/plain")];
                status_code = 500;
            });
        };
    };

};

You need to return upgrade: true in the response to the http_request request.

2 Likes

Thank you very much, it was it indeed.