Rust equivalent to simple requests.get for ES version 8?

I found the leap to version 8 big enough in Python, not least because I know nothing about authentication, certificates, proxies, etc.. I'm now trying to get the simplest possible response from a local server running version 8 using Rust. NB my current operative ES version is 7.10.2, on port 9200. ES server running version 8 I start manually, running on port 9500.

This works OK on Python:

    username = 'mike12'
    password = 'mike12'

    from requests.auth import HTTPBasicAuth
    kwargs['auth'] = HTTPBasicAuth(username, password)

    # set kwargs['verify'] to the CA (certification authority) certificate path
    # ES_PATH_CONF in the system turns out to be for 7.10.2 currently (2023-03-05), so set to the 8.6.2 path:
    os.environ['ES_PATH_CONF'] = r'D:\apps\ElasticSearch\elasticsearch-8.6.2\config'
    es_path_conf = os.getenv('ES_PATH_CONF')
    kwargs['verify'] = fr'{es_path_conf}\certs\http_ca.crt'

    try:
        response = requests.request('get', 'https://localhost:9500', **kwargs)
    except BaseException as e:
        ...

This is my attempt so far using Rust:

use reqwest::Client;
use std::io::Read;
use std::fs::File;
use tmp_env::set_var;
...
    let login_fields = [
        ("user", "mike12"),
        ("script", "true"),
        ("token", "[TOKEN]"),
        ("password", "mike12"), // is this how to add a password???
    ];
    
    let es_path = r#"D:\apps\ElasticSearch\elasticsearch-8.6.2\config"#;
    set_var("ES_PATH_CONF", &es_path);
	
    let mut buf = Vec::new();
	println!("buf {}", str_type_of(&buf));
	let cert_path = format!(r#"{es_path}\certs\http_ca.crt"#);
	println!("cert_path |{}|", cert_path);
    let mut cert_file = File::open(cert_path)?;
	println!("cert_file type {}", str_type_of(&cert_file));
	cert_file.read_to_end(&mut buf);
	println!("buf len now {}", buf.len());

    // let cert = reqwest::Certificate::from_der(&buf)?; // threw error   
    let cert = reqwest::Certificate::from_pem(&buf)?; // delivers cert OK
    println!("cert {:?} type {}", cert, str_type_of(&cert));
    
    let client = Client::builder()
        .add_root_certificate(cert)
        .build()?;
	
    let login_response = client
        .get("https://localhost:9500")
        .form(&login_fields)
        .send()
        .await?
        .text()
        .await?;

    println!("{}", login_response);

I get as far as the request. It returns the following:

{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}

Any ideas about what I'm doing wrong?

Ah, got it. basic-auth and drop form

    let login_response = client
        .get("https://localhost:9500")
        .basic_auth("mike12", Some("mike12"))
        .send()
        .await?
        .text()
        .await?;

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.