1

I am trying to retrieve and parse a JSON file using reqwest.

I used this question as a starting point but it doesn't work with my API.

The error:

Error: reqwest::Error { kind: Decode, source: Error("expected value", line: 1, column: 1) }
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let resp = reqwest::get("https://tse.ir/json/MarketWatch/data_7.json")
    .await?
    .json::<serde_json::Value>()
    .await?;

  println!("{:#?}", resp);
  Ok(())
}

The API works fine with other languages. thank for your help.

Cargo.toml:

[package]
name = "rust_workspace"
version = "0.1.0"
edition = "2021"

[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
reqwest = { version = "0.11", features = ["json", "blocking"] }
tokio = { version = "1", features = ["full"] }
bytes = "1"
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
  • This is an error in the JSON itself. Which you haven't provided. – Peter Hall Oct 30 '22 at 10:33
  • 3
    For example, you could show us what the response looks like as a string, by replacing `.json()` with `.text()` and adding the output to the question. – Peter Hall Oct 30 '22 at 10:45

1 Answers1

0

The error happens usually when the response doesn't contain

"content-type":"application/json"

Even though the content is a valid json you will get that error.

To solve the issue you need to use

let text_response = reqwest.get("...").await?.text().await?;
let resp: serde_json::Value = serde_json::from_str(&text_response)?;
Oussama Gammoudi
  • 685
  • 7
  • 13