0

I'm quite new to rust and try to parse a simple application/json API response into a struct. I'm using reqwest as HTTP client, following my code:

main.rs:

use reqwest;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct HttpbinResponse {
    origin: String,
    url: String,
}
fn main() {
    let client = reqwest::blocking::Client::new();
    let res = client
        .get("https://httpbin.org/get")
        .send()
        .expect("Error calling API");

    let httpbin_response: HttpbinResponse = res.json().expect("HTTPbin response parsing error");
    println!("{:?}", httpbin_response.url);
}

Cargo.toml:

[package]
name = "api-client"
version = "0.1.0"
edition = "2021"

[dependencies]
reqwest = { version = "0.11.13", features = ["blocking", "json"] }
tokio = "1.24.1"
serde = { version = "1.0.152", features = ["derive"] }

This code produces the following output: "https://httpbin.org/get"

Why do the double quotes remain in the variable? I would rather expect that parsing a JSON response should put the string values as pure strings into a struct, without the double quotes.

cafce25
  • 15,907
  • 4
  • 25
  • 31
pfust75
  • 401
  • 1
  • 5
  • 17
  • 1
    That's just how the debug formatter (`"{:?}"`) formats strings, maybe you meant to use `"{}"` as format string? – cafce25 Jan 14 '23 at 13:51

0 Answers0