Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to invoke a HTTP service #243

Open
AlbertoVPersonal opened this issue Nov 8, 2024 · 0 comments
Open

How to invoke a HTTP service #243

AlbertoVPersonal opened this issue Nov 8, 2024 · 0 comments
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers P1

Comments

@AlbertoVPersonal
Copy link

AlbertoVPersonal commented Nov 8, 2024

Describe the feature/proposal

Include a section to invoke a HTTP service.

Sample code

Cargo.toml

reqwest = "0.12.9"
dapr = "0.15.1"
tokio = "1.41.1"

After several optimizations, my Cargo.toml file is this way and it is running:

reqwest = { version = "0.12.9", default-features = false }
dapr = "0.15.1"
tokio = "1.41.1"

Rust code

This example uses:

  • query parameter
  • header value
tokio::runtime::Builder::new_current_thread()
    .enable_all()
    .build()
    .unwrap()
    .block_on(async {
        let client_result = dapr::Client::<dapr::client::TonicClient>::connect(format!("{}", ADDR)).await;
        let method_to_call = format!("<method-name-to-invoke>/{}", value);
        match client_result {
            Ok(mut client) => {
                // Create a new HTTP client
                let client = reqwest::Client::builder()
                    .timeout(Duration::from_secs(2)) // the value what you need
                    .build()
                    .unwrap();

                // Post the method
               // ADDR = "127.0.0.1"
               // PORT = <app-port-to-invoke>
                let url = format!("{}:{}/{}", ADDR, PORT, method_to_call);
                let req = client.post(url).header("dapr-app-id", "<app-id-to-invoke>");

                let response_result = req.send().await;
                match response_result {
                    Ok(response) => {
                        let body_result = response.text().await;
                        match body_result {
                            Ok(body) => {
                                println!("Response: {:?}", body);
                            },
                            Err(e) => {
                                eprintln!("e1: {}", e);
                            }
                        }
                    },
                    Err(e) => {
                        eprintln!("e2: {}", e);
                    }
                }
            },
            Err(e) => {
                eprintln!("Dapr client error: {}", e);
            }
        }
    });

Release Note

RELEASE NOTE: ADD New example to invoke to a HTTP service in the Rust SDK.

@mikeee mikeee added documentation Improvements or additions to documentation good first issue Good for newcomers P1 labels Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers P1
Projects
None yet
Development

No branches or pull requests

2 participants