I'm trying to make a POST request to use google cloud vision and I'm using reqwest for that. I need to provide my user account keys (here auth.json), but I get a authorization error. Since I can make the request and get a response with no problem using cURL, I think the problem might be in my code, specially in the way I include the file. Is there a way to do it properly?
Thanks very much in advance,
pub async fn req(url: &str) -> Result<String, Box<dyn std::error::Error>> {
let client = reqwest::Client::builder().build()?;
let request = client
.post("https://vision.googleapis.com/v1/images:annotate")
.header("Authorization", "Bearer -d @auth.json")
.header("Content-Type", "application/json; charset=utf-8")
.json(&(vec![build_request(url)]));
println!("{:?}", request);
let response =request
.send()
.await?;
let t = response
.text()
.await?;
Ok(t)
}
curl is a command-line program. It has its own syntax for flags and arguments. curl -d @auth.json is just curl's own syntax for reading a file. It hasn't got anything to do with HTTP or anything like that. If you put "Bearer -d @auth.json" in a Rust string literal, and send it to the server, then it will literally send that string. It won't magically read the file.
You'll have to read the file and put its contents into the header. For example:
let auth_token = std::fs::read_to_string("auth.json")?;
let request = client
.post("https://vision.googleapis.com/v1/images:annotate")
.header("Authorization", format!("Bearer {}", auth_token))
.header("Content-Type", "application/json; charset=utf-8")
.json(&(vec![build_request(url)]));