英文:
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() -> io::Result<()> {
let file = File::open("foo.txt")?;
let reader = BufReader::new(file);
for line in reader.lines() {
request::get("https://www.example.com/@{}", 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!("https://www.example.com/@{}", 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!("https://www.example.com/@{}", line?))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论