Hi I am making a http request from a motoko canister and I am setting max_response_bytes=2
The request only sends successfully if I send 21_000_000_000 cycles or more. Otherwise it throws this error
Caused by: The replica returned a rejection error: reject code CanisterReject, reject message http_request request sent with XXXX cycles, but 20_850_434_800 cycles are required., error code None
Cool whatever. However, that is a lot of cycles and considering the max_response_bytes are so small I would expect to see a lot of those cycles refunded to the canister but I never do.
Also, when I make this call I am actually seeing the canisters cycles drop by a lot more than just 21B its more like 63B cycles
If i comment out the function below in my code and hit the endpoint again i only use up 3M cycles
private func _sendRequestToUserbook(
path : Text,
body : ?Blob,
cycles : ?Nat,
) : async Bool {
let request_headers = [
{
name = Constants.USERBOOK_AUTH_HEADER_NAME;
value = env.userbook_auth_token;
},
{ name = "Content-Type"; value = "application/octet-stream" },
{ name = "Accept"; value = "text/plain" },
];
let principal = Principal.fromActor(this);
Debug.print("url: " # env.userbook_base_url # Principal.toText(principal) # "/" # path);
let http_request_args : IC.HttpRequestArgs = {
url = env.userbook_base_url # Principal.toText(principal) # "/" # path;
max_response_bytes = ?2;
headers = request_headers;
method = #get;
body = body;
// transform = null;
transform = ?{
function = transform;
context = Blob.fromArray([]);
}; // Apply the transform to remove headers
};
//2. ADD CYCLES TO PAY FOR HTTP REQUEST
//IC management canister will make the HTTP request so it needs cycles
//See: https://internetcomputer.org/docs/current/motoko/main/cycles
//The way Cycles.add() works is that it adds those cycles to the next asynchronous call
//See:
// - https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-http_request
// - https://internetcomputer.org/docs/current/references/https-outcalls-how-it-works#pricing
// - https://internetcomputer.org/docs/current/developer-docs/gas-cost
Cycles.add<system>(
switch (cycles) {
case (?value) { value };
case (null) { 21_000_000_000 };
}
);
let http_response : IC.HttpRequestResult = await IC.IC.http_request(http_request_args);
Debug.print("http response: " # debug_show (http_response));
if (http_response.status != 200) {
return false;
};
let decoded_text : Text = switch (Text.decodeUtf8(http_response.body)) {
case (null) { "No value returned" };
case (?y) { y };
};
Debug.print(decoded_text);
if (decoded_text == "OK") {
return true;
};
return false;
};
Can anyone help me with this.