Full endpoint for API Calls?

I’m trying to make the most simple API call to the IC’s via HTTPS interface. But when I try the following for some reason it downloads a file: https://ic0.app/api/v2/status/ Is this endpoint wrong or does it need some parameters?

How do I make an HTTPS API call from a browser?

Or can API calls via the HTTPS interface only be done via a CLI and not using a browser? Any clarification would be greatly appreciated!

You can make pure HTTPS calls (e.g. from curl), but there are some caveats.

For the moment only query calls are possible (no updates).
You will need to implement a function called http_request in your capsule, and add the needed logic there. I haven’t done it in motoko yet, but here’s a barebones rust example that I tested a while ago:

#[derive(CandidType, Clone, Deserialize, Debug)]
struct HeaderField(pub String, pub String);

#[derive(CandidType, Deserialize, Debug)]
struct HttpRequest {
    pub method: String,
    pub url: String,
    pub headers: Vec<HeaderField>,
    #[serde(with = "serde_bytes")]
    pub body: Vec<u8>,
}

#[derive(CandidType, Deserialize)]
struct HttpResponse {
    status_code: u16,
    headers: Vec<HeaderField>,
    #[serde(with = "serde_bytes")]
    body: Vec<u8>,
    streaming_strategy: Option<u32>,
}

#[query]
fn http_request(request: HttpRequest) -> HttpResponse {
    ic_cdk::println!("{:?}", request);

    HttpResponse {
        status_code: 200,
        headers: vec![],
        body: request.body.to_vec(),
        streaming_strategy: None,
    }
}

The above code should echo back whatever you send it.

1 Like

I literally stumbled upon this repo 5 minutes ago.

I’m not sure if this will help but you may want to give it a read.

2 Likes

Do you have an example of simple query call like a GET call that could be done via in a browser without additional coding? For example, calling/querying the following API endpoint from a browser gives you a list of all country info in a database: https://restcountries.eu/rest/v2/all

IC API calls via the HTTPS interface can be called from anywhere that https calls can be made. this endpoint: /api/v2/status/ is a correct one but it gives back bytes that are in a cbor-coding. You can check out this document for the full HTTPS api and for the encodings/decodings of the requests/responses of the endpoints: The Internet Computer Interface Specification :: Internet Computer

2 Likes

Got it - thanks for the explanation.