Skip to content
Sign upLog in
← Back to Community

How to make an HTTP request in c++ within 20 lines of code.

Profile icon
AbaanShanid

TLDR: This is the code:

system("curl https://example.com");

I am not highly experienced in c++, but when I found out about the system function, I made this super easy way to make an HTTP request! Hope this helps people. Check out the code, it's easy to understand. This uses the system function to make a get request using CURL. It can be easily customized to use it however you want including to make different type of requests like POST.

PS : Like this post? check out my other posts

Happy coding!

Voters
Profile icon
rubinskiy
Profile icon
usuarinexistent
Profile icon
Highwayman
Profile icon
figglediggle
Profile icon
AbaanShanid
Comments
hotnewtop
Profile icon
GrayKenobi

Question: I'm about a month or 2 into learning C++ so I'm new but is cout allowed? It clearly to works, but I hadn't heard about it before now. I've just been using std::cout. This would speed up my coding so much btw.

Profile icon
Highwayman

The short answer is: yes, if you include using namespace std; or using std::cout;, but I don't recommend it.
Basically cout and std::cout are the same thing. The reason you have to write std::cout is because cout is defined within the std(standard) namespace. bottom line is xx::xxx is the best way to refer to something if you can. look up c++ namespaces to learn more.

@GrayKenobi

Profile icon
GrayKenobi

Ok thnx for the explanation. I looked into it a bit further and I've decided to keep using std in front of the cout, seems that "using namespace std;" will just make things harder for myself. I'm glad I learned about this, it clears up any future questions I might have had about namespaces.

@Highwayman

Profile icon
programmeruser

Using system is jus using CURL to make a request, you're not creaing he request yourself. Use sys/socket.h to send a HTTP request through sockets

Profile icon
Highwayman

hm....
*makes unix socket*
*pipes curl to unix socket*
perfect. totally how it works.


@programmeruser