GET - Retrieve Data
To retrieve data from a homeserver, use the get
method on the PublicStorage
API. This allows you to read data from any user without authentication.
async fn get() -> pubky::Result<()> {
let pubky = Pubky::new()?;
// The public key of the user whose data you want to retrieve
let user_pubkey = PublicKey::try_from("o4dksfbqk85ogzdb5osziw6befigbuxmuxkuxq8434q89uj56uyy")
.expect("Valid user public key");
// The path to the data you want to retrieve
let url = format!("pubky://{}/pub/example.txt", user_pubkey);
// GET the content from the homeserver
let response = pubky.public_storage().get(url).await?;
let content = response.bytes().await?;
println!("Retrieved content: {:?}", content);
Ok(())
}
The GET operation:
- Uses the
public_storage()
API from yourPubky
instance - Takes a
pubky://
URL that specifies the public key and path - Returns the data stored at that location
- Can read data from any public key (if the data is public)
- Returns standard HTTP response with bytes
Listing Directory Contents
To list the contents of a directory:
async fn list() -> pubky::Result<()> {
let pubky = Pubky::new()?;
// The public key of the user whose data you want to list
let user_pubkey = PublicKey::try_from("o4dksfbqk85ogzdb5osziw6befigbuxmuxkuxq8434q89uj56uyy")
.expect("Valid user public key");
// List contents of a path
let url = format!("pubky://{}/pub/", user_pubkey);
let response = pubky.public_storage().get(url).await?;
let listing = response.text().await?;
println!("Directory listing: {}", listing);
Ok(())
}
Directory listings work the same way - just use a path ending with /
to list the contents of that directory.