reqwest http get 请求中的变量

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

variable in reqwest http get request

问题

我试图用Rust编写一个程序,使用reqwest在文件的每一行中发送一个HTTP请求,并将该行作为GET请求的一部分

```rust
use std::fs::File;
use std::io::{self, prelude::*, BufReader};
use reqwest;

fn main() -> io::Result<()> {
    let file = File::open("foo.txt")?;
    let reader = BufReader::new(file);

    for line in reader.lines() {
        let url = format!("https://www.example.com/{}", line?);
        let _response = reqwest::get(&url)?;
    }

    Ok(())
}

当我尝试这样做及其变体时,它说reqwest::get只接受一个参数,而我给了两个。


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

Im trying to make a program in rust that will send a http req with reqwest for every line in a file, and have that line be apart of the get request.

```use std::fs::File;
use std::io::{self, prelude::*, BufReader};

fn main() -&gt; io::Result&lt;()&gt; {
    let file = File::open(&quot;foo.txt&quot;)?;
    let reader = BufReader::new(file);

    for line in reader.lines() {
        request::get(&quot;https://www.example.com/@{}&quot;, line)
    }

    Ok(())
}

when I try this and variations of it, it says that reqwest::get only takes one parameter and I've given two.

答案1

得分: 1

我相信您想要格式化 URL 请求。例如:

request::get(format!("https://www.example.com/@{}", line))
英文:

I believe you want to format! the url request. For example:

request::get(format!(&quot;https://www.example.com/@{}&quot;, line))

答案2

得分: 0

您可以使用 [`?` 运算符](https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator) 来解包该行(它是一个 `Result`),然后使用 [`format!`](https://doc.rust-lang.org/std/macro.format.html) 来连接这些字符串。

```rust
request::get(format!("https://www.example.com/@{}", line?))
英文:

You can use the ? operator to unwrap the line (which is a Result) and use format! to concatenate the strings.

request::get(format!(&quot;https://www.example.com/@{}&quot;, line?))

huangapple
  • 本文由 发表于 2023年7月14日 00:52:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681711.html
匿名

发表评论

匿名网友

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

确定