elastic-rs / elastic

An Elasticsearch REST API client for Rust
Apache License 2.0
253 stars 40 forks source link

basic auth not working #386

Closed tanhaipeng closed 5 years ago

tanhaipeng commented 5 years ago

I use basic auth to build client, but find auth is not working( username & password is correct), my code is:

let auth = HeaderValue::from_str("username:password")?;
    let builder = SyncClientBuilder::new()
        .static_node("http://xxxx.com:9200")
        .params_fluent(move |p| p
            .url_param("pretty", true)
            .header(AUTHORIZATION, auth.clone()));
    let client = builder.build()?;

    let res = client
        .search::<Value>()
        .index("devlog")
        .body(json!({
            "query": {
                "query_string": {
                    "query": "*"
                }
            }
        }))
        .send()?;

Error is: next_error: Some(Parse(ParseError { inner: UnknownApiError({"reason": String("Invalid username and password!")

Please help me!

tanhaipeng commented 5 years ago

@KodrAus Please see this issue, thanks

DevQps commented 5 years ago

I got it working using this code!:

let mut builder = SyncClientBuilder::new().static_node(es_host);
let credentials = "<Base64 encoded: username:password>"
let parameter = PreRequestParams::new();
let header = AUTHORIZATION;
let value = HeaderValue::from_str(format!("Basic {}", credentials).as_str())?;
let parameter = parameter.header(header, value);
let builder - builder.params(parameter);

let client = match builder.build() {
    Err(x) => panic!("Error: {}", x),
    Ok(client) => client,
};

Hope that helps :) And let's give @KodrAus a bit of slack :) He's already working so hard on this project after all!

tanhaipeng commented 5 years ago

I got it working using this code!:

let mut builder = SyncClientBuilder::new().static_node(es_host);
let credentials = "<Base64 encoded: username:password>"
let parameter = PreRequestParams::new();
let header = AUTHORIZATION;
let value = HeaderValue::from_str(format!("Basic {}", credentials).as_str())?;
let parameter = parameter.header(header, value);
let builder - builder.params(parameter);

let client = match builder.build() {
    Err(x) => panic!("Error: {}", x),
    Ok(client) => client,
};

Hope that helps :) And let's give @KodrAus a bit of slack :) He's already working so hard on this project after all!

good! Thank you very much!

mwilliammyers commented 4 years ago

As of: #401 (6ed243570bb64fdf398cafe0518c5d9a02321c76) you can now do:

use elastic::client::SyncClientBuilder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let http_client = reqwest::Client::builder()
        .danger_accept_invalid_certs(true)
        .build()?;

    let client = SyncClientBuilder::new()
        .static_node("https://localhost:9201")
        .http_client(http_client)
        .params_fluent(|p| p.basic_auth("admin", Some("admin")).unwrap())
        .build()?;

    println!("{:#?}", client.ping().send()?);

    Ok(())
}