为 reqwest 客户端构建具有多个值的参数的查询字符串
Build query string with param having multiple values for a reqwest Client
我正在尝试使用构建器的 query() 方法为 reqwest 客户端构建查询字符串。但是参数之一必须是像这样编码的多个值
?dt=t&dt=at&dt=m
我想不通。
use reqwest::blocking::Client;
use serde_json::{json, Value as JsonValue};
fn main() {
let query = json!({
"client": "gtx",
"ie": "UTF-8", // input encoding
"oe": "UTF-8", // output encoding
"sl": "auto", // source language
"tl": "en", // target language
"dt": ["t", "at", "m"], // <<<<< ERROR
"q": "salut les gars. ca va? on y va!", // text to translate
});
let client = Client::new();
let response = client.get("https://translate.googleapis.com/translate_a/single")
.query(&query)
.send()
.unwrap();
if response.status().is_success() {
let body: JsonValue = response.json().unwrap();
println!("detected language {:}", body.get(2).unwrap());
for item in body.get(0).and_then(JsonValue::as_array).unwrap() {
println!("{:}", item[1]);
println!("{:}", item[0]);
}
} else {
println!("fail {:?}", response);
}
}
reqwest::Error { kind: Builder, source: Custom("unsupported value") }
可能不支持?我想我可以用格式手动构建字符串!万不得已
我不熟悉Rust,但是查询结构一定是这样的:
?dt[]=t&dt[]=at&dt[]=m
否则你只是在覆盖变量。
在 PHP 中你有 http_query_builder。也许这会给您带来正确的方向:https://docs.rs/querystring/latest/querystring/ ?
您可以将查询参数构建为 IntoIterator
对 key-value 对。
例如,
let query = vec![
("client", "gtx"),
("ie", "UTF-8"), // input encoding
("oe", "UTF-8"), // output encoding
("sl", "auto"), // source language
("tl", "en"), // target language
("dt", "t"),
("dt", "at"),
("dt", "m"),
("q", "salut les gars. ca va? on y va!"), // text to translate
];
let client = Client::new();
let response = client.get("https://translate.googleapis.com/translate_a/single")
.query(&query)
.send()
我正在尝试使用构建器的 query() 方法为 reqwest 客户端构建查询字符串。但是参数之一必须是像这样编码的多个值
?dt=t&dt=at&dt=m
我想不通。
use reqwest::blocking::Client;
use serde_json::{json, Value as JsonValue};
fn main() {
let query = json!({
"client": "gtx",
"ie": "UTF-8", // input encoding
"oe": "UTF-8", // output encoding
"sl": "auto", // source language
"tl": "en", // target language
"dt": ["t", "at", "m"], // <<<<< ERROR
"q": "salut les gars. ca va? on y va!", // text to translate
});
let client = Client::new();
let response = client.get("https://translate.googleapis.com/translate_a/single")
.query(&query)
.send()
.unwrap();
if response.status().is_success() {
let body: JsonValue = response.json().unwrap();
println!("detected language {:}", body.get(2).unwrap());
for item in body.get(0).and_then(JsonValue::as_array).unwrap() {
println!("{:}", item[1]);
println!("{:}", item[0]);
}
} else {
println!("fail {:?}", response);
}
}
reqwest::Error { kind: Builder, source: Custom("unsupported value") }
可能不支持?我想我可以用格式手动构建字符串!万不得已
我不熟悉Rust,但是查询结构一定是这样的:
?dt[]=t&dt[]=at&dt[]=m
否则你只是在覆盖变量。
在 PHP 中你有 http_query_builder。也许这会给您带来正确的方向:https://docs.rs/querystring/latest/querystring/ ?
您可以将查询参数构建为 IntoIterator
对 key-value 对。
例如,
let query = vec![
("client", "gtx"),
("ie", "UTF-8"), // input encoding
("oe", "UTF-8"), // output encoding
("sl", "auto"), // source language
("tl", "en"), // target language
("dt", "t"),
("dt", "at"),
("dt", "m"),
("q", "salut les gars. ca va? on y va!"), // text to translate
];
let client = Client::new();
let response = client.get("https://translate.googleapis.com/translate_a/single")
.query(&query)
.send()