# Why does the HTTP authentication with Elasticsearch fail in Rust?

**URL:** <https://discuss.elastic.co/t/why-does-the-http-authentication-with-elasticsearch-fail-in-rust/228033>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 15, 2020, 5:23am UTC](https://discuss.elastic.co/t/why-does-the-http-authentication-with-elasticsearch-fail-in-rust/228033 "2020-04-15T05:23:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikasmastud](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vikasmastud/32/53059_2.png) [@vikasmastud](https://discuss.elastic.co/u/vikasmastud)\
**Post date:** [April 15, 2020, 5:23am UTC](https://discuss.elastic.co/t/why-does-the-http-authentication-with-elasticsearch-fail-in-rust/228033/1 "2020-04-15T05:23:59Z")

</div>

When I use `let client = Elasticsearch::default()` , it works fine. But when I try to add credentials for the client:

```auto
#[macro_use]
extern crate serde_json;
use elasticsearch::{
    auth::Credentials,
    http::transport::{SingleNodeConnectionPool, Transport, TransportBuilder},
    params::Refresh,
    Elasticsearch, Error, IndexParts, SearchParts,
};
use futures::executor::block_on;
use serde_json::Value;
use url::Url;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let credentials = Credentials::Basic("<username>".into(), "<password>".into());
    let u = Url::parse("http://localhost:9200")?;
    let conn_pool = SingleNodeConnectionPool::new(u);
    let transport = TransportBuilder::new(conn_pool).auth(credentials).build()?;
    let client = Elasticsearch::new(transport);
    let response = client
        .search(SearchParts::Index(&["tweets"]))
        .from(0)
        .size(10)
        .body(json!({
            "query": {
                "match": {
                    "message": "Elasticsearch rust"
                }
            }
        }))
        .send()
        .await?;

    let response_body = response.read_body::<Value>().await?;

    println!("{:?}", response_body);

    Ok(())
}

```

I get the following error:

```auto
response Object(
    {
        "error": String(
            "Not Found",
        ),
        "message": String(
            "Not Found",
        ),
        "statusCode": Number(
            404,
        ),
    },
)

```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 15, 2020, 11:22am UTC](https://discuss.elastic.co/t/why-does-the-http-authentication-with-elasticsearch-fail-in-rust/228033/2 "2020-04-15T11:22:58Z")

</div>

The response isn't one that I would expect from Elasticsearch. For example, If I run your code against Elasticsearch 7.5.2 instance that **doesn't** contain a tweets index, the response is

```auto
Object({"error": Object({"index": String("tweets"), "index_uuid": String("_na_"), "reason": String("no such index [tweets]"), "resource.id": String("tweets"), "resource.type": String("index_or_alias"), "root_cause": Array([Object({"
index": String("tweets"), "index_uuid": String("_na_"), "reason": String("no such index [tweets]"), "resource.id": String("tweets"), "resource.type": String("index_or_alias"), "type": String("index_not_found_exception")})]), "type":
 String("index_not_found_exception")}), "status": Number(404)})

```

Note that `error` is a `serde::Value::Object` and there's no `statusCode` or `message` properties.

My toolchain is

```auto
$ rustup show

Default host: x86_64-pc-windows-msvc
rustup home: <home>

installed toolchains
--------------------

stable-x86_64-pc-windows-gnu
stable-x86_64-pc-windows-msvc
nightly-x86_64-pc-windows-gnu
nightly-x86_64-pc-windows-msvc (default)

active toolchain
----------------

nightly-x86_64-pc-windows-msvc (default)
rustc 1.44.0-nightly (f509b26a7 2020-03-18)

```

Some questions:

1. When you say it _works_ with `Elasticsearch::default()`, what's the response?
2. What version of Elasticsearch are you running against, and is auth configured?
3. Do you have a tweets index set up?
4. If you configure a proxy on the transport and capture the request/response, can you share what you get?
5. Please can you show your toolchain (`rustup show`) and Cargo.toml config for the client.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 15, 2020, 12:36pm UTC](https://discuss.elastic.co/t/why-does-the-http-authentication-with-elasticsearch-fail-in-rust/228033/3 "2020-04-15T12:36:44Z")

</div>

Based on the similarity of the response to [Invalid NEST response built from a successful (404) low level call](https://discuss.elastic.co/t/invalid-nest-response-built-from-a-successful-404-low-level-call/228088), it looks like you may have seeded the client with the Kibana endpoint; the client requires the Elasticsearch endpoint 👍

---

<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:** [May 13, 2020, 12:36pm UTC](https://discuss.elastic.co/t/why-does-the-http-authentication-with-elasticsearch-fail-in-rust/228033/4 "2020-05-13T12:36:44Z")

</div>

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