How to parse reqwest::blocking::response json to a struct

Hi all
Real newbie trying to learn Rust ... Started off to write a basic little program that basically just queries and API and then should parse the response. I am having real trouble getting deceriazing the json response into a struct.
Here is the code so far :

extern crate reqwest;
use reqwest::header::{HeaderMap , HeaderValue, AUTHORIZATION};
use serde_derive::{Serialize, Deserialize};  
use serde_json;
use serde::Deserialize;

#[derive(Deserialize)]
struct Confidence {
    min_confidence : i32,
    average_configende : i32,
    max_confidence : i32,
}

fn main() {
    let ioc = "value to be searched";
    let api_key = "api key details";
    let base_url= format!("https://domain.com/?type=confidence&value={}", ioc) ;  // Join the &str and String to create full URL
    let full_url = &base_url[..];   // Convert String to &str which is required by reqwest::blocking::client 

    let mut request_header = HeaderMap::new();
    request_header.insert( AUTHORIZATION, HeaderValue::from_static(api_key));

    let client = reqwest::blocking::Client::new();
    let mut response_full = client.get(full_url)
    .headers(request_header)
    .send();   
//println!("Printing Body :  {:#?}" , response.unwrap().text() ) ;   // prints the body 

If i un-comment the println this is the output :
Body : Ok(
"{"min_confidence": 0, "average_confidence": 0, "max_confidence": 0}",
)

I think by now i have tried at least 10 different 'ways' of trying to get the data into the stuct using various json methods ( just copying/editing lots of examples i have found on the internet that look familiar ) and had 0 luck .

Any help would be greatly appreciated , i have tried readin the crate documentation pages for reqwest and serde but it's just confused me more

Please edit your post to follow our code formatting guidelines.

You should be able to just do

let value: Confidence = response.json().unwrap();

apologies, fixed it

Nah doesnt work.
no method named json found for enum std::result::Result<reqwest::blocking::Response, reqwest::Error> in the current scope
method not found in std::result::Result<reqwest::blocking::Response, reqwest::Error>

If it helps this is what my cargo file looks like

[dependencies]

reqwest = { version = "0.10.0-alpha.2", features = ["blocking" ,"json"] }
serde = "1.0.114"
serde_json = "1.0"
serde_derive = "1.0.114"

You need to .unwrap() the response. Whenever it says "no method ... found for enum Result<..., ...>" then that means that you called a fallible operation, and need to handle that failure.

1 Like

Damn so simple , thanks so much and sorry for wasting ur time.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.