Skip to content

How to invoke a HTTP service #243

Open
@AlbertoVPersonal

Description

@AlbertoVPersonal

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions