How to make http_request in canister with right enough cycles?

maybe i didnt calcute right ?

(
  "The http_request resulted into error. RejectionCode: 
CanisterReject, Error: http_request request sent with 
176_928_000 cycles,
 but 20_933_203_200 cycles are required.",
)

its about 120times bigger cycles than i expect.

//https://internetcomputer.org/docs/current/developer-docs/gas-cost
pub fn calculate_cost(
  node_num: u32,
  send_bytes: u64,
  receive_bytes: u64,
) -> u128 {
  let base_fee_http_request_init =
    (3_780_000 * node_num) as u128 * node_num as u128;
  let request_cost = 400 * node_num as u128 * send_bytes as u128;
  let response_cost = 800 * node_num as u128 * receive_bytes as u128;
  base_fee_http_request_init + request_cost + response_cost
}

}


Questions:

  1. If cycles can not be too determined, then Can i just send with 1T cycles and after http_request it return rest of cycles for me ?

  2. Also i found out if i exec http_request in local dfx enviroment. even 1 cycle can excute a big http_request . so wired. Which cause me a bug if i want to make http_request on ic-main-net. :smiling_face_with_tear:

  3. And also i guess some of cycle cost can be a part of sdk lib to auto generate for developers ?

my solution so far :

#[ic_cdk::update]

pub fn some_http_call(cycles:u128)->String{
...
}

by excute some_http_call(0) . the call with response with how many cycles it inneeded. so make a new call some_http_call(x)

Yes, that’s possible

You’re most likely running your local replica in system subnet mode, where nothing costs anything. It’s either configured like that in dfx.json or in the place where dfx info networks-json-path points you to

But even if you run in application subnet mode (what you’re probably looking for), then the local costs won’t be the same as on mainnet since your local subnet then only contains 1 node instead of 13. Here’s the gas cost table if you want more on that

We decided against that because we can’t offer something that works completely. IMO overpaying is the easiest way to do it for now

Yeah, since overpaying is a good way so far . Totally agree!
Thanks for so detailed and clear answer ! @Severin