alexcrichton / curl-rust

Rust bindings to libcurl
MIT License
1.02k stars 235 forks source link

Digest Auth problem #308

Closed hortonberman closed 5 years ago

hortonberman commented 5 years ago

on command line run curl : curl -u kkkkk:yyyyy --digest -X POST http://127.0.0.1:80/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"create_address","params":{"account_index":0,"label":"new-sub"}}' -H 'Content-Type: application/json'

this woks.but follow code. rut it . 401 Unauthorized.

let mut body = r#"{"jsonrpc":"2.0","id":"0","method":"create_address","params":{"account_index":0,"label":"new-sub"}}"#.as_bytes();

let mut easy = Easy::new();

easy.url("http://127.0.0.1:80/json_rpc").unwrap();

let mut auth=Auth::new();

auth.digest(true);

easy.http_auth(&auth).unwrap();

easy.username("kkkkk").unwrap();
easy.password("yyyyy").unwrap();

   let mut list = List::new();
list.append("Content-Type: application/json").unwrap();
easy.http_headers(list).unwrap();

easy.post(true).unwrap();
easy.post_field_size(body.len() as u64).unwrap();

let mut data = Vec::new();
{
    // Create transfer in separate scope ...
    let mut transfer = easy.transfer();

    // Request body
    transfer.read_function(|buf| {
        Ok(body.read(buf).unwrap_or(0))
    }).unwrap();

    // Response body
    transfer.write_function(|new_data| {
        data.extend_from_slice(new_data);
        Ok(new_data.len())
    }).unwrap();

    transfer.perform().unwrap();
    // .. to force drop it here, so we can use easy.response_code()
}

println!("{}", easy.response_code().unwrap());
println!("Received {} bytes", data.len());
if !data.is_empty() {
    println!("Bytes: {:?}", data);
    println!("As string: {}", String::from_utf8_lossy(&data));
}

run above code. 401 Unauthorized, what happend? curl-rust don't support digest Auth?

alexcrichton commented 5 years ago

Thanks for the report! I unfortunately can't offer much help in terms of debugging why curl itself isn't working, I'd recommend reading curl documentation and user questions/etc for libcurl itself, and possibly contacting the upstream libcurl project for assistance.