How to: Http request #415
-
Hello, I've been trying to send https request from a lambda function and I'm having the hardest time ever. Please find below what I've tried, what is working and what is not working. Using Reqwest to make it easy. If you have any example to point me to or could explain what I'm doing wrong, it would be much appreciated, thank you! What is working: let body = reqwest::get(uri)
.await?
.text()
.await?;
info!("{}", body); What is not working: let client = reqwest::Client::new();
let res = client.get(uri).send().await?;
match res.status() {
n @ StatusCode::OK => {info!("{}", res.text().await?);
Ok::<String, Error>(format!("{}", n))},
n => Ok::<String, Error>(format!("{}", n.as_str()))
}; I'm getting the following error message:
But the following compile on rust Playground: use reqwest::{StatusCode, Client, Error};
use log::{info};
async fn foo() -> Result<String, Error>{
let client = Client::new();
let uri = "";
let res = client.get(uri).send().await?;
match res.status() {
n @ StatusCode::OK => {info!("{}", res.text().await?);
Ok::<String, Error>(format!("{}", n))},
n => Ok::<String, Error>(format!("{}", n.as_str()))
}
} any idea? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @Chandrian! There shouldn't be any difference whether or not you're running this code in a Lambda function. What version of Rust/Cargo are you currently using? I'm thinking that you might be using an older version of Rust that cannot properly infer that For reference, I wrote a small Lambda function using your code and I can compile it without any problem, using version 1.58.1: use reqwest::{Client, StatusCode};
use lambda_runtime::{Context};
type Error = Box<dyn std::error::Error + Send + Sync>;
#[tokio::main]
async fn main() -> Result<(), Error> {
lambda_runtime::run(lambda_runtime::handler_fn(my_handler)).await?;
Ok(())
}
async fn my_handler(_value: serde_json::Value, _ctx: Context) -> Result<String, Error> {
let client = Client::new();
let uri = "";
let res = client.get(uri).send().await?;
match res.status() {
n @ StatusCode::OK => {println!("{}", res.text().await?);
Ok::<String, Error>(format!("{}", n))},
n => Ok::<String, Error>(format!("{}", n.as_str()))
}
} |
Beta Was this translation helpful? Give feedback.
Hi @Chandrian!
There shouldn't be any difference whether or not you're running this code in a Lambda function. What version of Rust/Cargo are you currently using?
I'm thinking that you might be using an older version of Rust that cannot properly infer that
reqwest::Response
is safe to use sinceStatusCode
implements Copy.For reference, I wrote a small Lambda function using your code and I can compile it without any problem, using version 1.58.1: