The following requirements are mandatory:
- A recent C++ compiler which fully supports C++11
- CMake
Note that this program depends on some libraries. CMake
first download those libraries and then it builds the program.
You can use the graphical interface of CMake (cmake-gui
) to generate a makefile
. If you prefer command line interface, first go to the project path and then execute the following commands:
$ mkdir build
$ cd build
$ cmake ..
$ make
For more information about using CMake check out this link.
You can use the graphical interface of CMake to generate a project (for example Visual Studio project). If you prefer command line interface, first go to the project path and then execute the following commands:
mkdir build
cd build
cmake ..
Then you can open the generated project with your favorite IDE (e.g. Visual Studio). For more information about using CMake check out this link.
You can see various examples in the following line. For more information about available arguments run ./httpc --help
.
$ ./httpc -v get "http://httpbin.org/get?course=networking&assignment=1"
$ ./httpc -v post "http://httpbin.org/post" -h "Content-Type: application/json" -d '{"Assignment": 1}'
$ ./httpc -v post "http://httpbin.org/post" -h "Content-Type: application/json" -f sample_files/post_data_1.txt
$ ./httpc -v post "http://httpbin.org/post" -h "Content-Type: application/json" -f sample_files/post_data_2.txt
$ ./httpc -v post "http://httpbin.org/post" -h "Content-Type: application/json" -h "User-Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0" -h "Accept-Language: en-US,en;q=0.7,fa;q=0.3" -f sample_files/post_data_2.txt
$ ./httpc -v post "http://httpbin.org/post" -h "Content-Type: application/x-www-form-urlencoded" -f sample_files/post_data_3.txt
You can find some sample files for --file
option in sample_files
directory.
I implemented both absolute and relative redirection.
$ ./httpc -v --redirect get "http://httpbin.org/redirect-to?url=http://example.com"
$ ./httpc -v --redirect get "http://httpbin.org/redirect/6"
$ ./httpc -v --redirect --max-redirection=3 get "http://httpbin.org/redirect/6"
$ ./httpc -v --redirect get "http://httpbin.org/relative-redirect/6"
$ ./httpc -v --redirect get "http://httpbin.org/absolute-redirect/6"
We can store both text and binary files using -o
flag.
$ ./httpc -v get "http://httpbin.org/image/png" -o image.png
$ ./httpc -v get "http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_surround-fix.avi" -o big_buck_bunny.avi
Using --header-file
, you can associate the content of a file as headers for HTTP request. Since there should be an empty line between header request and the body request in POST, it can be tricky. In my implementation user can put empty lines at the end of fine but the program eliminate them. Here is an example:
$ ./httpc -v post "http://httpbin.org/post" --header-file sample_files/header_data_1.txt -f sample_files/post_data_3.txt
I've used the following external libraries. You don't need to download them. It's the job of CMake:
- Asio for cross-platform socket programming
- cxxopts for command line interface
- LUrlParser for parsing URLs