Rust server to listen request and send them to the GitHub API

Hello! I'm a newbie in Rust and I just need a little help if possible because I'm in trouble...

I'm trying to have a Rust server to listen on an IP address.
When the server got a GET request, I want to send this GET request to the github API and then return the result on the web page.
In this code I'm trying to send the request to https://api.github.com/users/octocat and just display the json response on the web page.

How can I display the result on the web page?
How can I change the user when I am requesting the server?

I tried to mix things I found on the internet, but my code is broken ^^' So if you can give me some advices to work on it it would be very helpful!

async fn get_function(user : String) -> Result<impl warp::Reply, warp::Rejection> {

      let address = String::from("https://api.github.com/users/");
      let HttpRequest = [address, user].concat();
    
      let resp = reqwest::Client::new()
          .get(HttpRequest)
          .send()
          .await?
          .json::<std::collections::HashMap<String, String>>()
          .await?;
         
      println!("{:#?}", resp);
    
      Ok(())
        
   }     
        
  #[tokio::main]
  async fn main() -> Result<(), Box<dyn std::error::Error>> {
    
    let user = String::from("octocat");
    let get_request = warp::get().and(get_function(user));
            
    warp::serve(get_request)
      .run(([127, 0, 0, 1], 3030))
      .await;
                  
   Ok(())
}

Instead of printing to the terminal, return the response from the function.

async fn get_function(user: String) -> Result<impl warp::Reply, warp::Rejection> {
    let address = String::from("https://api.github.com/users/");
    let HttpRequest = [address, user].concat();

    let resp = reqwest::Client::new()
        .get(HttpRequest)
        .send()
        .await?
        .json::<std::collections::HashMap<String, String>>()
        .await?;

    let mut output_string = String::new();
    write!(output_string, "{:#?}", resp).unwrap();

    Ok(output_string)
}

In this case I use write! to write the data to a String instead of printing to terminal.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.