HTTP requests in Rust on Windows

Hi there!
For background, I'm pretty new to Rust but quite fluent in Python and have a decent knowledge of C and x86-64 asm. I gather that Rust's standard library doesn't include any high level HTTP facilities and it is conventional to depend on third party libraries like reqwest or something else. I'm quite uncomfortable using third party libraries (only in low level programmes i.e C and in this case Rust) because you can't do anything useful without libraries in Python. But when I need a functionality that's not available in C standard library (for example Internet/compression decompression related facilities) there's the system api to help. All I have to do is to #Include <Windows.h> and you get access to a boat load of facilities from the Win32 api.

Again there's curl for HTTP related stuff and zlib for compression & decompression but I find myself leaning towards Win32 when I use C. So how can I make this happen? Like write a programme that send a HTTP Get request to say downloads page, parses the HTML body, finds the installed version on my system, and lists all the new versions available? (The part I need help here is the HTTP requests part).

Plese do not suggest libraries. I'm open to write a client from scratch I just need a little assistance and perhaps point me somewhere I could look for more information! Will it be too complicated than it's C equivalent? Or should I use C FFIs? Or perhaps look into GitHub - microsoft/windows-rs: Rust for Windows ?

P.S I went through the reqwest, hyper crate's source code in github to figure how they've implemented it but found that to be overwhelming since I do not have much experience with Rust to comprehend complex projects. Hope I'm not being too ambitious for a newbie.

Thank you.

If you like to rely on Win32 when coding C, and you don't care about portability, then you indeed might want to take a closer look at the windows-rs crate. A minor warning though .. I'm not sure how much flux it is in now, but in the past it was pretty common to have to change your code for each update of the windows crate.

IIRC there's a simple HTTP tutorial in the Rust Book -- perhaps that's a good place to start?

I'm curious to know why not? You admit being happy to use libraries in Python and such. Whatever Windows provides is not actually part of the C language or it's standard libraries so it seem you are happy to use MS libraries there. Why the aversion to libraries (crates) in Rust?

At the end of the day if the functionality one wants does not exist one has to create it or use something that somebody else has made already, a library. Life is too short to write everything. In my case it's most likely if I were to try they would be worse quality and more insecurity prone than something written by a domain expert.


Thank you. Appreciate it. I'll sure look into Windows-rs but over a superficial look it seems ripe with unsafe blocks and basically looks like a Rust wrapper around Win32 lol. And yes I do not care about portability since I write programmes for my own use, not for clients, so the overhead of making my code portable is an unnecessary mental labour for me. (I do not programme for a living). I'm currently using Programming Rust by Jim Blandy et. al. as a reference. But sure I'll try the Rust book. Thanks again.

The reason is that I'm a little biased and prejudiced. I explicitly stated that I prefer Win32 over well respected C libraries like curl or zlib because Win32 is solid, like ROCK SOLID and has a consistent style and the linking is effortless since MSVC will do it for you (unless you are compiling using the commandline) so that you don't have to pass the .lib and header file directories to the linker not to mention different coding styles from different libraries making a mess in your programme. Win32 almost always uses camel case with occasional trailing underscores. About three months ago I played around Win32 GUI programming using Charlez Petzold's Programming Windows book (written in 1998) and the C code worked without any changes. Imo Win32 is gold except for their clumsy typedefs. Say whatever you will about Microsoft but I admire their stance on backwards compatibility (not so much common in their .NET stuff).

And for the Python libraries part, the libraries I use are pretty much the standard in Python ecosystem. Like NumPy, Pandas, matplotlib, beautifulsoup... these are time tested libraries. (But an experienced Rust programmer may say the same about reqwest or other crates) Imho Rust crates aren't up to that level.

Besides I try low level language to learn what's really happening under the hood. Otherwise what I was trying to do will take 10 lines maximum in Python. And high level wrappers/libraries actually hurt the learning process.

That's all. I have nothing against these crate authors or contributors It's just not my cup of tea.

1 Like

To be honest, that kind of contradicts that you don't want a crate. That's what crates like reqwest do, they provide a kind of higher-level abstraction. And I'd say it is pretty battle-tested.

Fair enough. I don't mind people preferring X over Y. Provided the preference is not justified by false claims about either.

When calling from Rust into anything else unsafe is required. After all you are transferring control from the safe world (as Rust defines it) into the unsafe world. I imagine the Windows API to have hundreds (thousands) of functions so any interface to it will necessarily be ripe with unsafe blocks.

A wrapper around Win32 is what you have ordered is it not? Either you have to make one or use one somebody else has made already.

Only enough the last, and last, time I toyed with the Windows API, back in the 1990's, I did it in assembler. I did not understand C++ at the time (even less now). Worked really well. Not sure I learned much about what is under the hood though.

Say whatever you will about Microsoft but I admire their stance on backwards compatibility

Same, and this is the main reason I've never really made the jump to Linux or MacOS. I love the fact that any code I write for Windows will probably still work two decades from now.

(But an experienced Rust programmer may say the same about reqwest or other crates)

Yep, pretty much! Rust crates (obviously) haven't been around as long as venerable Python libraries, but they are solid and reliable.

Besides I try low level language to learn what's really happening under the hood

Good philosophy; I agree :slight_smile: Also hyper is probably not a great guide, since it's async, and you probably don't want to go down the async route just yet.

So yes, as you say, windows-rs is basically a wrapper around Win32, and you'll have to write a lot of unsafe code to use it. The strong convention in Rust is to then create safe wrappers around your unsafe code. "Programming Rust" describes how to do this.

In your case, I guess you'd start with the socket API: WSASocket(), WSARecv(), etc. I assume these functions exist in windows-rs, so then you'd create a safe Rust interface to them.

I planned on leaving this thread as it was but came back just because I wanted to appreciate you being nice. I really appreciate the assistance and will certainly look into the windows-rs crate. Thanks again.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.