0

我是 Rust 的新手,并试图使用reqwestcrate 调用 API。我希望我的代码;

  1. 在 GET 请求中包含“Content-Disposition: Inline”标头。
  2. 返回 JSON 并将其打印为文本

我不确定我做错了什么,希望有人可以帮助我识别它。

该请求在 JS 和 Postman 中对我来说很好,并返回数组 JSON 数据,也在我下面的 RUST 代码中请求返回 200 成功。

但是我看不到响应数据,远程服务器使用"Content-Disposition"默认的标头响应(不是我指定的“内联”)。

我的 Rust 代码编译得很好,似乎可以发送:

extern crate reqwest;
use reqwest::header::HeaderName;
use reqwest::header::HeaderValue;
use reqwest::header::ACCEPT;
use reqwest::header::CONTENT_DISPOSITION;

fn main() {
    let client = reqwest::Client::new();
    let response_text = client
        .get("http://nemlog.com.au/show/1h/NEMLOG.json?id1=RRP.DISPATCHPRICE0&k1=vic")
        .header(CONTENT_DISPOSITION, "inline")
        .header(ACCEPT, "*/*")
        .send()
        .unwrap();

    println!("Response Text: {:#?}", response_text);
}

我从上述请求在控制台中收到的响应是:

Response Text: Response {
  url: "http://nemlog.com.au/show/1h/NEMLOG.json?id1=RRP.DISPATCHPRICE0&k1=vic",
  status: 200,
  headers: {
    "date": "Tue, 22 Oct 2019 06:33:45 GMT",
    "server": "Apache/2.4.6 (CentOS) PHP/5.4.16 mod_wsgi/3.4 Python/2.7.5",
    "content-disposition": "attachment; filename=NEMLOG.json",
    "transfer-encoding": "chunked",
    "content-type": "text/json; name=\"NEMLOG.json\"",
  },
}

我希望看到的输出是来自调用的内联响应,如下所示:

{"SETTLEMENTDATE":"2019-10-22T10:45:00Z","REGIONID":"VIC1","RRP.DISPATCHPRICE0":91.272}
{"SETTLEMENTDATE":"2019-10-22T10:50:00Z","REGIONID":"VIC1","RRP.DISPATCHPRICE0":75.7859}
{"SETTLEMENTDATE":"2019-10-22T10:55:00Z","REGIONID":"VIC1","RRP.DISPATCHPRICE0":74.8683}
{"SETTLEMENTDATE":"2019-10-22T11:00:00Z","REGIONID":"VIC1","RRP.DISPATCHPRICE0":25.14}
{"SETTLEMENTDATE":"2019-10-22T11:05:00Z","REGIONID":"VIC1","RRP.DISPATCHPRICE0":16.4812}
{"SETTLEMENTDATE":"2019-10-22T11:10:00Z","REGIONID":"VIC1","RRP.DISPATCHPRICE0":56.4572}
{"SETTLEMENTDATE":"2019-10-22T11:15:00Z","REGIONID":"VIC1","RRP.DISPATCHPRICE0":38.5679}
{"SETTLEMENTDATE":"2019-10-22T11:20:00Z","REGIONID":"VIC1","RRP.DISPATCHPRICE0":47.5941}
{"SETTLEMENTDATE":"2019-10-22T11:25:00Z","REGIONID":"VIC1","RRP.DISPATCHPRICE0":25.14}
{"SETTLEMENTDATE":"2019-10-22T11:30:00Z","REGIONID":"VIC1","RRP.DISPATCHPRICE0":51.8465}
{"SETTLEMENTDATE":"2019-10-22T11:35:00Z","REGIONID":"VIC1","RRP.DISPATCHPRICE0":71.7957}
{"SETTLEMENTDATE":"2019-10-22T11:40:00Z","REGIONID":"VIC1","RRP.DISPATCHPRICE0":72.0757}
4

3 回答 3

0

send是懒惰的:它在收到回复标头后立即返回,而无需等待正文。您可以使用该方法获取文本,该text方法会等到收到全文,或者您可以使用Read实现来分块处理它:

let mut response = client
    .get("http://nemlog.com.au/show/1h/NEMLOG.json?id1=RRP.DISPATCHPRICE0&k1=vic")
    .header(CONTENT_DISPOSITION, "inline")
    .header(ACCEPT, "*/*")
    .send()
    .unwrap();

println!("Response: {:#?}", response);
println!("Response text: {}", response.text().unwrap());

至于content-disposition回复,您确定您的服务器尊重content-disposition标头吗?你能检查你用 JS 和 Postman 得到的响应头吗?

于 2019-10-22T10:09:13.010 回答
0

谢谢彼得和奥马尔。

我设法解决了这个问题,答案是您的建议的组合。我需要将以下内容添加到要发送的代码中,解开响应然后运行 ​​text 方法:

.send()
.unwrap()
.text();

非常感谢两位!

于 2019-10-23T04:34:01.453 回答
-1

我对 rust 和 reqwest 都没有深入的了解,但据我所知,您将 Response 对象转换为文本并打印没有消息正文或有效负载的 Response 结构。通过在 Google 上的快速搜索,我找到了一个text()属于 Response 对象的方法,它应该为您提供有效负载。

这是链接: https ://docs.rs/reqwest/0.8.4/reqwest/struct.Response.html

这可能是一个解决方案:

extern crate reqwest;
use reqwest::header::HeaderName;
use reqwest::header::HeaderValue;
use reqwest::header::ACCEPT;
use reqwest::header::CONTENT_DISPOSITION;

fn main() {
    let client = reqwest::Client::new();
    let response_text = client
        .get("http://nemlog.com.au/show/1h/NEMLOG.json?id1=RRP.DISPATCHPRICE0&k1=vic")
        .header(CONTENT_DISPOSITION, "inline")
        .header(ACCEPT, "*/*")
        //.send() <-- it seems that you do not have to send explicitly
        .text();  // <-- !!!!!!! Change

    println!("Response Text: {:#?}", response_text);
}
于 2019-10-22T08:23:07.807 回答