How do you call http_request on localhost?

From the recent challenge to find the secret, i am wondering how we can call the http_request in local deployment?

public query func http_request() : async HttpResponse {
return {
status_code = 200;
headers = [(“content-type”, “text/plain”)];
body = T.encodeUtf8 (
“This is nomeata’s capture-the-ic-token canister.\n” #
“See GitHub - nomeata/capture-the-ic-token: Hack the canister, get the token for details.\n” #
“\n” #
"My current cycle balance: " # debug_show (ExperimentalCycles.balance()) # “\n” #
"Secret loaded from random tape: " # debug_show (O.isSome(secret)) # “\n” #
"Successful calls to set_certified_data: " # debug_show successful_calls # “\n” #
"Unsuccessful calls to set_certified_data: " # debug_show unsuccessful_calls # “\n”
)
}
};

One way is to use icx-proxy to forward HTTP requests.

3 Likes

Can you clarify how you would call the icx-proxy locally? For example, let’s say I have multiple canisters deployed on my local replica, and only one of them has implemented http_request. How do I target that canister locally using HTTP?

Typically in prod, you would make an HTTP request to https://<canister-id>.raw.ic0.app, where canister-id is the ID of the canister that implemented http_request.

However when testing locally, the domain in the URL is the address that the local icx-proxy is listening on, something like https://localhost:8080. Where would you specify the canister ID in this case?

The way I did it for now was to edit /etc/hosts to add an entry like:

127.0.0.1 myproject.localhost

Then start icx-proxy like this:

icx-proxy --fetch-root-key --address 127.0.0.1:8453 --dns-alias myproject.localhost:rrkah-fqaaa-aaaaa-aaaaq-cai -v -v

That way I can make HTTP requests to http://myproject.localhost:8453. You can pick a different host name and port.

1 Like

Hmm, don’t you also need to tell icx-proxy where the local replica is listening on? Like with a flag --replica http://localhost:$(cat .dfx/replica-configuration/replica-1.port)

I haven’t been doing that locally so I guess the default value matches the local replica.

2 Likes

Holy crap, it worked. Thanks a bunch!

Actually, you can skip the /etc/hosts step and use the IP address directly in the --dns-alias flag, e.g. --dns-alias 127.0.0.1:<canister_id>.

1 Like

Yes, sorry I didn’t make that clear.

Also, you may find this useful: feat: upgrade HTTP request calls from query to update upon canister's request by paulyoung · Pull Request #19

How do you configure it on the main network,
I got an error when I replaced dns-alias with https://ic0.app
Error: Text must be a base 32 string.

I think you need to make requests to https://your-canister-id.ic0.app

I get the same error when I request this URL with cmd
icx-proxy --debug --address 127.0.0.1:8453 --dns-alias https://d4svy-5yaaa-aaaai-qam4q-cai.ic0.app

Once things are deployed to the network you wouldn’t need to use icx-proxy and would make HTTP requests directly to https://your-canister-id.ic0.app instead

Got it - thanks for the explanation.