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

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

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:

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

This gives me the error:

  1. let my_string = body.to_string();
  2. | ^^^^^^^^^ method cannot be called on `&lambda_http::Body` due to unsatisfied trait bounds
  3. What am I doing wrong, and how should I be reading the docs?"
  4. <details>
  5. <summary>英文:</summary>
  6. 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.
  7. 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?
  8. Current code:
  9. async fn process_request(request: Request) -&gt; Result&lt;impl IntoResponse, std::convert::Infallible&gt; {
  10. let body = request.body();
  11. let my_string = body.to_string();
  12. if let Err(e) = write_to_dynamodb(&amp;my_string).await {
  13. eprintln!(&quot;Error: {}&quot;, DisplayErrorContext(e));
  14. }
  15. }
  16. This gives me the error:
  17. let my_string = body.to_string();
  18. | ^^^^^^^^^ method cannot be called on `&amp;lambda_http::Body` due to unsatisfied trait bounds
  19. What am I doing wrong, and how should I be reading the docs?
  20. </details>
  21. # 答案1
  22. **得分**: 1
  23. 请看以下翻译:
  24. 自 [`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()`):
  25. ```rust
  26. let body = request.body();
  27. 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):

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

答案2

得分: 1

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

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

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:

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

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:

确定