0

I am new to Rust and trying to call an API using the reqwest crate. I want my code to;

  1. Include with the GET request the 'Content-Disposition: Inline' header.
  2. Return and print the JSON as text

I'm not sure what I'm doing wrong and hoping someone can help identify it for me.

The request works fine for me in JS and in Postman and returns the array JSON data, also in my RUST code below the request returns a 200 success.

But I can't see the response data & the remote server responds with the "Content-Disposition" header default (not 'inline' as I specify).

My Rust Code which compiles fine and seems to send ok:

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);
}

the response I recieve in the console from the above request is:

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\"",
  },
}

The output I wish to see is the inline response from the call which looks like this:

{"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}
Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
simwilso
  • 23
  • 6

3 Answers3

0

I have neither deep knowledge of rust nor reqwest, but from what I see, you transform the Response object to text and print the Response structure without the message body or payload. With quick search on Google, I found a method text() belonging to the Response object which should give you the payload.

Here is the link: https://docs.rs/reqwest/0.8.4/reqwest/struct.Response.html

That might be a solution:

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);
}
Peter Paul Kiefer
  • 2,114
  • 1
  • 11
  • 16
  • I have no problem with down voting my answer. But I see nothing wrong with it. I'm no rust guru, but I can read the manuals. And it was useful for the OP. So why mark this answer as not valuable and keeping other from getting a useful information or link? And if there is a mistake why just down vote. Why don't you write a comment what's wrong and help others. From my point of view: I got a message that there is something wrong with my answer but I don't know what it is. – Peter Paul Kiefer Oct 28 '19 at 18:14
0

send is lazy: it returns as soon as the reply headers are received without waiting for the body. You can get the text with the text method, which does wait until the full text is received, or you can use the Read implementation to process it in chunks:

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());

As for the content-disposition of the reply, are you sure that your server honors the content-disposition header? Can you check the response headers you get with JS and Postman?

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
Jmb
  • 18,893
  • 2
  • 28
  • 55
  • Thanks Omer. I managed to fix this issue and it was a combination of what you and Peter have described so thanks alot. – simwilso Oct 23 '19 at 04:27
0

Thanks Peter and Omer.

I managed to get this fixed and the answer was a combination of your suggestions. I needed to add the following to my code to send, unwrap the response then run the text method:

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

Thanks so much both!

simwilso
  • 23
  • 6