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

**URL:** <https://discuss.elastic.co/t/rust-equivalent-to-simple-requests-get-for-es-version-8/342180>\
**Category:** Elasticsearch\
**Created:** [September 3, 2023, 7:59am UTC](https://discuss.elastic.co/t/rust-equivalent-to-simple-requests-get-for-es-version-8/342180 "2023-09-03T07:59:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrodent](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@mrodent](https://discuss.elastic.co/u/mrodent)\
**Post date:** [September 3, 2023, 7:59am UTC](https://discuss.elastic.co/t/rust-equivalent-to-simple-requests-get-for-es-version-8/342180/1 "2023-09-03T07:59:02Z")

</div>

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:

```auto
    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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![mrodent](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@mrodent](https://discuss.elastic.co/u/mrodent)\
**Post date:** [September 3, 2023, 8:48am UTC](https://discuss.elastic.co/t/rust-equivalent-to-simple-requests-get-for-es-version-8/342180/2 "2023-09-03T08:48:27Z")

</div>

Ah, got it. `basic-auth` and drop `form`

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 1, 2023, 8:49am UTC](https://discuss.elastic.co/t/rust-equivalent-to-simple-requests-get-for-es-version-8/342180/3 "2023-10-01T08:49:20Z")

</div>

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