Receiving GET request in rust canister

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

#[derive(CandidType, Deserialize)]
pub struct HttpRequest {
    pub url: String,
    pub method: String,
    pub body: serde_bytes::ByteBuf,
    pub headers: Vec<HeaderField>,
}

#[derive(CandidType, Deserialize)]
pub struct HttpResponse {
    pub body: serde_bytes::ByteBuf,
    pub headers: Vec<HeaderField>,
    pub status_code: u16,
}

#[query]
fn http_request(req: HttpRequest) -> HttpResponse {
    HttpResponse {
        status_code: 200,
        headers: vec![HeaderField("Content-Type".into(), "text/plain".into())],
        body: serde_bytes::ByteBuf::from(b"No image found".to_vec()),
    }
}



Candid:


type HeaderField = record { text; text; };
type HttpRequest = record {
    method: text;
    url: text;
    headers: vec HeaderField;
    body: blob;
};
type HttpResponse = record {
    status_code: nat16;
    headers: vec HeaderField;
    body: blob;
};

service : () -> {
  http_request: (request: HttpRequest) -> (HttpResponse) query;
}


When trying to access canister at https://canister_id.icp0.io/

I receive: Error 503 Response Verification Error

What am I doing wrong?

<canister>.ic0.app [Edit: or <canister>.icp0.io>] forces you to use asset certification to prevent malicious replicas from responding with non-consensus-approved responses. If you want to skip the certification (only recommended for debugging), then you can use <canister>.raw.ic0.app [Edit: or <canister>.raw.icp0.io]

2 Likes

It’s been ages since I last used raw, so just in case it’s a typo: did you mean raw.icp0.io?

In addition to @Vivienne answer, it can also be verified by “forcing” the response to be treated as an update (documentation).

#[query]
fn http_request(req: HttpRequest) -> HttpResponse {
    HttpResponse {
        status_code: 200,
        headers: vec![HeaderField("Content-Type".into(), "text/plain".into())],
        body: serde_bytes::ByteBuf::from(b"No image found".to_vec()),
        update: true // <------------
    }
}

Just mentionning it for testing purposes as it’s maybe also not what you’d want to use in your actual solution.

4 Likes