GithubHelp home page GithubHelp logo

andsonye / multipartencoder Goto Github PK

View Code? Open in Web Editor NEW
65.0 4.0 20.0 80 KB

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

License: MIT License

Makefile 11.25% C++ 87.08% Shell 1.67%
cpprestsdk http-client multipart form-data

multipartencoder's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.