Http_request how to not upgrade for raw domain?

I read the documentation about the upgrade to update calls for http_request and http_request_update.

I had a few try with a sample repo, a canister on mainnet (https://okoji-rqaaa-aaaap-qasma-cai.raw.ic0.app/) and I do indeed notice that I have to set upgrade = true in the http_request response if I want to provide update calls and therefore support the service worker. Otherwise it throws an error Body does not pass verification.

However now, I am confuse about how to NOT set upgrade = true, i.e. set update = false, for the raw domain?

How can I detect that the request is made for the raw domain and therefore the update call can be spared!?!?

(pseudo code)

public shared query func http_request(request : HttpRequest) : async HttpResponse {
        {
            upgrade = ?true; // <----- here how to set false or true?!?!
            body = Blob.toArray(Text.encodeUtf8("Yolo"));
            headers;
            status_code = 200;
            streaming_strategy = null;
        };    
};

  public shared func http_request_update(request : HttpRequest) : async HttpResponse {
    {
            upgrade = ?false; // <----- here always false or null right?
            body = Blob.toArray(Text.encodeUtf8("Yolo"));
            headers;
            status_code = 200;
            streaming_strategy = null;
        };    
  };

Note: in this feature I want the canister to provide the same content, an index.html file, over .raw.ic0.app and .ic0.app. It’s a “custom asset canister”.

Just to avoid any confusion;

The purpose of the upgrade field is to “upgrade” a query call to an update call so that you can change state in a HTTP request.

It’s not about upgrading from raw to non-raw.

Does that change anything about what you’re asking?

1 Like

Yes it does but then I’m confuse why the service worker fails :thinking:

If I don’t implement the upgrade = true and the method http_request_update then when I access the non raw domain canister-id.ic0.app the service worker fails with following error

Body does not pass verification

e.g.

Fail: https://6zvwc-sqaaa-aaaal-aalma-cai.ic0.app/

Ok: https://okoji-rqaaa-aaaap-qasma-cai.ic0.app/ (from sample repo)

The reason is that the service worker expects a certification header to be set by the canister using certified variables when you do http_request (query call). An update call on the other hand goes through consensus and is certified by default.

1 Like

Gotcha. So either I generate a certification header or back to my original question.

Is there a Motoko lib or sample code to generate such header?

Ah in this thread The "Body does not pass verification" error from a Motoko canister - #4 by nomeata @nomeata provided an example

2 Likes

Quite a long way until everything comes together in a final solution but, my question is solved by @domwoe answer and @nomeata sample repo.

I should not force the update calls but provide certified data.

It works out.

2 Likes