Skip to content

Add Bearer Token authentication support - #28

Open
Porrapat wants to merge 1 commit into
jeremychone:mainfrom
Porrapat:main
Open

Add Bearer Token authentication support#28
Porrapat wants to merge 1 commit into
jeremychone:mainfrom
Porrapat:main

Conversation

@Porrapat

@Porrapat Porrapat commented Aug 28, 2026

Copy link
Copy Markdown

For now, we can add to use Bearer Token. We can do something like

use anyhow::Result;
use serde::Deserialize;
use serde_json::json;

const END_POINT: &str = "http://localhost:3000";

#[derive(Debug, Deserialize)]
struct LoginResponse {
    access_token: String,
}

#[tokio::main]
async fn main() -> Result<()> {
    let hc = httpc_test::new_client(END_POINT)?;

    let result = hc
        .do_post(
            "/api/login",
            json!({
                "email": "admin@website.com",
                "password": "password"
            }),
        )
        .await?;

    let login: LoginResponse = result.json_body_as()?;

    let access_token = login.access_token;
    println!("token = {}", access_token);

    let hc2 = httpc_test::new_client(END_POINT)?;
    let hcc2 = hc2.bearer_auth(access_token.clone());
    hcc2.do_get(
            "/api/dashboard",
        )
        .await?
        .print()
        .await?;

    hcc2.do_put(
            "/api/rename-something",
            json!({
                "title": "New Name",
            })
        )
        .await?
        .print()
        .await?;

    hcc2.do_post(
        "/api/logout",
        json!({})
    )
    .await?
    .print()
    .await?;

    Ok(())
}

Just the idea. I make this to use for myself.
Thank you for your wonderful open source software.

@Porrapat Porrapat changed the title Add support for Bearer Token feature - add new bearer_token feature. Add support for Bearer Token support - add new bearer_token feature. Aug 28, 2026
@Porrapat Porrapat changed the title Add support for Bearer Token support - add new bearer_token feature. Add support for Bearer Token - add new bearer_token feature. Aug 28, 2026
@Porrapat Porrapat changed the title Add support for Bearer Token - add new bearer_token feature. Add Bearer Token authentication support Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant