如何使用 reqwest 获得响应主体?
How to get body of response with reqwest?
我正在尝试向 Binance API 发送 GET 请求。但是我在我的终端中得到这个输出而不是数据:
Response { url: Url { scheme: "https", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("api.binance.com")), port: None, path: "/api/v3/exchangeInfo", query: Some("symbol=BNBBTC"), fragment: None }, status: 200, headers: {"content-type": "application/json;charset=UTF-8", "content-length": "1515", "connection": "keep-alive", "date": "Thu, 23 Dec 2021 23:28:24 GMT", "server": "nginx", "vary": "Accept-Encoding", "x-mbx-uuid": "1244d760-2c41-46df-910f-b95c4a312bc2", "x-mbx-used-weight": "10", "x-mbx-used-weight-1m": "10", "strict-transport-security": "max-age=31536000; includeSubdomains", "x-frame-options": "SAMEORIGIN", "x-xss-protection": "1; mode=block", "x-content-type-options": "nosniff", "content-security-policy": "default-src 'self'", "x-content-security-policy": "default-src 'self'", "x-webkit-csp": "default-src 'self'", "cache-control": "no-cache, no-store, must-revalidate", "pragma": "no-cache", "expires": "0", "access-control-allow-origin": "*", "access-control-allow-methods": "GET, HEAD, OPTIONS", "x-cache": "Miss from cloudfront", "via": "1.1 08b9c2fd11813ffdb8fa03129d0a465d.cloudfront.net (CloudFront)", "x-amz-cf-pop": "FRA56-C2", "x-amz-cf-id": "EBp6UQUM3B2Lz0iAoPM88INjL4C0ugIgxmaoTPzi0Q4WPxfG46p8Yw=="} }
我的代码如下所示:
async fn main() {
let client = Client::new();
let res = client.get("https://api.binance.com/api/v3/exchangeInfo?symbol=BNBBTC")
// .header(USER_AGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36")
// .header(CONTENT_TYPE, "application/json")
// .header(CACHE_CONTROL, "no-store")
// .header(PRAGMA, "no-cache")
.send().await;
println!("{:?}", res.unwrap());
}
我做错了什么?
您正在打印的 Response
基本上只是初始 HTTP 信息(例如状态和 headers)。您还需要根据您的期望使用方法等待负载:
bytes
/bytes_stream
/chunk
获取原始数据
text
/text_with_charset
以字符串形式获取数据
json
to deserialize the data into a structured type (see the docs for serde_json
了解更多信息)
在这种情况下,您似乎获得了 JSON 有效载荷,因此将 .json()
用于可反序列化类型听起来是正确的方法,但如果您的唯一目标是打印它那么 .text()
可能是更简单的方法。
async fn main() {
let client = Client::new();
let res = client
.get("https://api.binance.com/api/v3/exchangeInfo?symbol=BNBBTC")
.send()
.await
.expect("failed to get response")
.text()
.await
.expect("failed to get payload");
println!("{}", res);
}
相关:
我正在尝试向 Binance API 发送 GET 请求。但是我在我的终端中得到这个输出而不是数据:
Response { url: Url { scheme: "https", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("api.binance.com")), port: None, path: "/api/v3/exchangeInfo", query: Some("symbol=BNBBTC"), fragment: None }, status: 200, headers: {"content-type": "application/json;charset=UTF-8", "content-length": "1515", "connection": "keep-alive", "date": "Thu, 23 Dec 2021 23:28:24 GMT", "server": "nginx", "vary": "Accept-Encoding", "x-mbx-uuid": "1244d760-2c41-46df-910f-b95c4a312bc2", "x-mbx-used-weight": "10", "x-mbx-used-weight-1m": "10", "strict-transport-security": "max-age=31536000; includeSubdomains", "x-frame-options": "SAMEORIGIN", "x-xss-protection": "1; mode=block", "x-content-type-options": "nosniff", "content-security-policy": "default-src 'self'", "x-content-security-policy": "default-src 'self'", "x-webkit-csp": "default-src 'self'", "cache-control": "no-cache, no-store, must-revalidate", "pragma": "no-cache", "expires": "0", "access-control-allow-origin": "*", "access-control-allow-methods": "GET, HEAD, OPTIONS", "x-cache": "Miss from cloudfront", "via": "1.1 08b9c2fd11813ffdb8fa03129d0a465d.cloudfront.net (CloudFront)", "x-amz-cf-pop": "FRA56-C2", "x-amz-cf-id": "EBp6UQUM3B2Lz0iAoPM88INjL4C0ugIgxmaoTPzi0Q4WPxfG46p8Yw=="} }
我的代码如下所示:
async fn main() {
let client = Client::new();
let res = client.get("https://api.binance.com/api/v3/exchangeInfo?symbol=BNBBTC")
// .header(USER_AGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36")
// .header(CONTENT_TYPE, "application/json")
// .header(CACHE_CONTROL, "no-store")
// .header(PRAGMA, "no-cache")
.send().await;
println!("{:?}", res.unwrap());
}
我做错了什么?
您正在打印的 Response
基本上只是初始 HTTP 信息(例如状态和 headers)。您还需要根据您的期望使用方法等待负载:
bytes
/bytes_stream
/chunk
获取原始数据text
/text_with_charset
以字符串形式获取数据json
to deserialize the data into a structured type (see the docs forserde_json
了解更多信息)
在这种情况下,您似乎获得了 JSON 有效载荷,因此将 .json()
用于可反序列化类型听起来是正确的方法,但如果您的唯一目标是打印它那么 .text()
可能是更简单的方法。
async fn main() {
let client = Client::new();
let res = client
.get("https://api.binance.com/api/v3/exchangeInfo?symbol=BNBBTC")
.send()
.await
.expect("failed to get response")
.text()
.await
.expect("failed to get payload");
println!("{}", res);
}
相关: