Hi !
I recently started to explore the possibilities of creating my own API and so far, it is pretty much simple in Python and C#; that's why I wanted to dive a little deeper in it and try to create my own in C.
The problem is that it seems that I can't find any accessible and minimalist implementation of an API in C (with its basic concepts, such as socket
for example).
Could anyone show me / link me a resource that would explain me how to do a small hello world for example (as flask does) ?
Thank you !
Top comments (10)
Since you haven't specified what sort of API you are interested in, I've taken the liberty of assuming a simple line-based protocol (request/response) in this example, most higher layer textual protocols such as HTTP/SMTP/NNTP/IMAP/etc. are built on top of plain lines of text:
This 'works for me' on my local machine, compiled with vanilla gcc:
then in another terminal:
Enjoy :)
Thank you so much !
I will build my researches around this, thank you for your time !!
You're welcome - there are a few things I would say about writing network services in C, and particularly on unix-like systems:
Have fun!
Does it need to use socket? Even for C programming, writing an API using sockets can soon become non-portable and painful. I'd suggest using something like mongoose for C or Poco if you can stretch to C++ (the "other" poco if you're coming from C# ;)). The poco project has a hello world style web app example right on it's home page.
I would absolutely love to. Unfortunately it seems that "try it the way we teach it" instead of "do it in the most elegant way or with the most relevant tools" is not a very common practice in CS here 😭
I ended up going with a C# API in one of my projects, more so out of it's ease of authentication/authorization with Active Directory.
I found the .NET Web API framework really easy to use, but as Meghan mentioned, nothing is too "minimal" around here.
Nothing in C is minimalist.
True ahah
This is maybe a little of topic. But Ms build a really powerful rest framework in cpp. Maybe you can draw some inspiration from there
I will give it a look, thanks !