You may find the asynchronous http-client, i.e. cpprestsdk, does not support posting a multipart/form-data request. This MultipartEncoder is a work around to generate the body content of multipart/form-data format. So that then you can use a cpp HTTP-client, which is not limited to cpprestsdk, to post a multipart/form-data request by setting the encoded body content.
-
Clone the MultipartEncoder repository
# Make sure to clone with --recursive git clone --recursive https://github.com/AndsonYe/MultipartEncoder.git
-
If not cloned with --recursive, you need to manually get the cpprestsdk submodle
Ignore this step if you followed step 1 above.
git submodule update --init --recursive
-
Install dependencies required by cpprestsdk
sudo apt-get install g++ git make zlib1g-dev libboost-all-dev libssl-dev cmake
-
Build cpprestsdk. Suppose the directory you cloned MultipartEncoder into is
MultipartEncoder_ROOT
cd $MultipartEncoder_ROOT/cpprestsdk/Release mkdir build cd build cmake .. make -j$(nproc)
-
Build MultipartEncoder sample
cd $MultipartEncoder_ROOT make
-
Run the sample
./run.sh
The response is writtern in file $MultipartEncoder/results
MultipartParser parser; //Create parser instance;
parser.AddParameter(key, value); //Add text parameters using AddParameter
parser.AddFile(key, file_path); //Add file content using AddFile
std::string boundary = parser.boundary(); //Get the boundary generated by parser, each parser has its unique boundary, this boundary should be set in the HTTP request's header
std::string body = parser.GenBodyContent(); //Get the encoded multipart/form-data body content
Then you can use boundary
and body
in any HTTP clients.
Check the parser_test.cpp for complete example.