将lambda_http Body对象转换为字符串类型。

huangapple go评论54阅读模式
英文:

Convert lambda_http Body object to string type?

问题

以下是您要翻译的内容:

"I'm new to Rust. I have a lambda_http Request object and I would like to obtain the text of the Body as a string.

I'm reading the docs on Body but am too new to Rust to understand what I'm looking at. Possibly I need to use the Text attribute somehow?

Current code:

async fn process_request(request: Request) -> Result<impl IntoResponse, std::convert::Infallible> {
    let body = request.body();
    let my_string = body.to_string();
    if let Err(e) = write_to_dynamodb(&my_string).await {
        eprintln!("Error: {}", DisplayErrorContext(e));
    }
}

This gives me the error:

let my_string = body.to_string();
|                          ^^^^^^^^^ method cannot be called on `&lambda_http::Body` due to unsatisfied trait bounds

What am I doing wrong, and how should I be reading the docs?"

<details>
<summary>英文:</summary>

I&#39;m new to Rust. I have a lambda_http Request object and I would like to obtain the text of the Body as a string. 

I&#39;m reading the 
(https://docs.rs/lambda_http/0.1.1/lambda_http/enum.Body.html) but am too new to Rust to understand what I&#39;m looking at. Possibly I need to use the `Text` attribute somehow?
Current code: async fn process_request(request: Request) -&gt; Result&lt;impl IntoResponse, std::convert::Infallible&gt; { let body = request.body(); let my_string = body.to_string(); if let Err(e) = write_to_dynamodb(&amp;my_string).await { eprintln!(&quot;Error: {}&quot;, DisplayErrorContext(e)); } } This gives me the error: let my_string = body.to_string(); | ^^^^^^^^^ method cannot be called on `&amp;lambda_http::Body` due to unsatisfied trait bounds What am I doing wrong, and how should I be reading the docs? </details> # 答案1 **得分**: 1 请看以下翻译: 自 [`Body` 解引用为 `&amp;[u8]`](https://docs.rs/aws_lambda_events/0.7.3/src/aws_lambda_events/encodings.rs.html#253-260) ,请使用 [`std::str::from_utf8()`](https://doc.rust-lang.org/stable/std/str/fn.from_utf8.html)(如果你需要 `String` 而不是 `&amp;str`,可以调用 `to_owned()`): ```rust let body = request.body(); let my_string = std::str::from_utf8(body).expect("非 UTF-8 字符串");
英文:

Since Body derefs to &amp;[u8], use std::str::from_utf8() (you can call to_owned() if you want String and not &amp;str):

let body = request.body();
let my_string = std::str::from_utf8(body).expect(&quot;non utf-8&quot;);

答案2

得分: 1

在你提供的文档中,看起来"Body"是一个枚举类型。如果你想将它作为一个字符串处理,你首先需要检查枚举是否是文本类型,如下所示:

let body = request.body();
if let lambda_http::Body::Text(my_string) = body {
    println!("{}", my_string);
    // 在这里使用 my_string 进行操作
}
英文:

in the docs you linked, it looks like the Body is an enum. If you want it as a string, you'd first have to check if the enum is Text like this:

    let body = request.body();
    if let lambda_http::Body::Text(my_string) = body {
        println!(&quot;{}&quot;, my_string);
        // do stuff here with my_string
    }

huangapple
  • 本文由 发表于 2023年3月15日 21:25:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定