Search code examples
pythonrustpython-requestsreqwest

What is the Rust reqwest equivalent of the following Python requests code?


I am trying to do a Basic Authentication GET, ignoring certificate validation, using reqwests.

I have following Python requests code, which works:

import requests
from requests.auth import HTTPBasicAuth

session = requests.sessions.Session()
session.auth = HTTPBasicAuth("username", "password")
session.verify = False
response = session.get("http://myurl.com/myapi")

I want to do the same thing using Rust reqwest. So far, I have the following:

use reqwest

let url = "http://myurl.com/myapi";

let response = reqwest::Client::builder()
  .danger_accept_invalid_certs(true)
  .build()
  .unwrap()
  .get(&url)
  .basic_auth("username", Some("password"))
  .send()
  .await?;

The Python requests call works as expected. However, I get the following response from Rust:

Error: reqwest::Error { kind: Request, url: Url { scheme: "https", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("myurl.com")), port: None, path: "myapi", query: None, fragment: None }, source: Error { kind: Connect, source: Some("unsuccessful tunnel") } }

If I need to produce more code (I can't provide the actual URL), please let me know.


Solution

  • I would hardly consider this an answer, but I will post this just to update my solution.

    1. I first updated my Cargo.toml reqwest dependency to the following to remove TLS dependencies:

      reqwest = version = "0.12.4", default-features = false }
      
      • this was causing reqwest to throw a TooManyRedirects error.
      • Removing the default-features also disables the ability to control redirects, so this didn't work.
    2. Then, after resetting my Cargo.toml to:

      reqwest = "0.12.4"
      
    3. I updated the GET call, removing the .danger_accept_invalid_certs(true) line:

      let response = reqwest::Client::builder()
        .danger_accept_invalid_certs(true)
        .build()
        .unwrap()
        .get(&url)
        .basic_auth("username", Some("password"))
        .send()
        .await?;
      

    It's now working.

    Again, I don't know if that's necessarily a problem / problem solved, but I'm posting it just in case.