Haiku API: download a file via HTTP/HTTPS

I’m currently writing an application for Haiku which uses a dictionary database (sqlite3). I’d like to be able to update the dictionary from within the app by downloading the updated file over http or https. I could do it quite easily with libcurl but I’d rather use the native Haiku API.

I have looked at the documentation for the network kit but didn’t really find a good starting point except for BHttpRequest but I’m not sure if I’m supposed to use that directly or if there is some kind of layer above it.

It would be nice if someone could point me in the right direction where to start.

you can learn how to use it from the tests source

Hopefully we will get documentation written for it soon.

Normally you create requests from the BUrlProtocolRoster, which makes sure a request of the right type (HTTP, FTP, Gopher, …) is created. You can then either RunSycnhronously (it will block until the request terminates), or just Run() (it will run the request in a separate thread asynchronously). You have to listen to events using the appropriate listener in either case, either as direct callbacks, or as BMessages.

You have to handle DataReceived to store the data to disk (or wherever you want), take care of what happens with redirects (you get data both for the redirect page and for the target), handle HTTP errors, etc.

The test do not use a great patern, as they wait on the request to complete using polling. But you get an idea of the API from them. They also don’t use the protocol roster.

2 Likes

Thanks for the hint, @jjpx.

Thanks @PulkoMandy for the detailed explanation. I´ll try it and get back with a success report or more likely with some more questions :wink: