MultipartEncoder Save

C++ implementation of encoding HTTP multipart/form-data into a string buffer for POST action in HTTP clients

Project README

MultipartEncoder: A C++ implementation of encoding multipart/form-data

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.

Build & Run

  1. Clone the MultipartEncoder repository

    # Make sure to clone with --recursive
    git clone --recursive https://github.com/AndsonYe/MultipartEncoder.git
    
  2. 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
    
  3. Install dependencies required by cpprestsdk

    sudo apt-get install g++ git make zlib1g-dev libboost-all-dev libssl-dev cmake
    
  4. 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)
    
  5. Build MultipartEncoder sample

    cd $MultipartEncoder_ROOT
    make
    
  6. Run the sample

    ./run.sh
    

    The response is writtern in file $MultipartEncoder/results

Usage

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.

Open Source Agenda is not affiliated with "MultipartEncoder" Project. README Source: AndsonYe/MultipartEncoder
Stars
64
Open Issues
0
Last Commit
2 years ago
License
MIT

Open Source Agenda Badge

Open Source Agenda Rating