Embedded Enthusiast
Embedded Enthusiast

Reputation: 47

HTTP post request parser

I intend to parse http POST request received from a client in my server code. I am using Postman application to send file using the POST method onto the server. My question is how do I parse the POST request on the server side. My server code is in C++ The client would be sending a ~80MB file using the POST request. I have referred example codes but none of them show how to parse a POST request with an in-coming file.

https://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/example/http/server/

Could someone help point to a sample code for this?

Regards

Upvotes: 0

Views: 1871

Answers (1)

sehe
sehe

Reputation: 393134

Let's start accept a single connection on port 8081:

net::io_context io;
tcp::acceptor a(io, {{}, 8081});

tcp::socket s(io);
a.accept(s);

Now, let's read a HTTP request:

http::request<http::string_body> req;
net::streambuf buf;
http::read(s, buf, req);

That's all. We can print some details, and send a response. Let's say we want to save the uploaded file:

std::cout << "Writing " << req.body().size() << " bytes to " << fname << "\n";
std::ofstream(fname) << req.body();

Let's also send the entire payload back as the response content:

    http::response<http::string_body> response;
    response.reason("File was accepted");
    response.body() = std::move(req.body());
    response.keep_alive(false);
    response.set("XXX-Filename", fname);

    http::write(s, response);

FULL DEMO

Live On Coliru

#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <boost/beast/http.hpp>
#include <boost/optional/optional_io.hpp>
#include <iostream>
#include <fstream>

namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
using net::ip::tcp;
using namespace std::string_literals;

static std::string const fname = "upload.txt";

int main() {
    net::io_context io;
    tcp::acceptor a(io, {{}, 8081});

    tcp::socket s(io);
    a.accept(s);

    std::cout << "Receiving request from " << s.remote_endpoint() << "\n";

    http::request<http::string_body> req;
    net::streambuf buf;
    http::read(s, buf, req);

    std::cout << "Method: " << req.method() << "\n";
    std::cout << "URL: " << req.target() << "\n";
    std::cout << "Content-Length: "
        << (req.has_content_length()? "explicit ":"implicit ")
        << req.payload_size() << "\n";

    std::cout << "Writing " << req.body().size() << " bytes to " << fname << "\n";
    std::ofstream(fname) << req.body();

    {
        http::response<http::string_body> response;
        response.reason("File was accepted");
        response.body() = std::move(req.body());
        response.keep_alive(false);
        response.set("XXX-Filename", fname);

        http::write(s, response);
    }
}

When testing with the CLI POST utility (e.g. apt install libwww-perl on Ubuntu):

POST http://localhost:8081/this/url?id=$RANDOM -H 'Host: demo.site' -H 'CustomHeader' -E -C 'user:password' < test.cpp

It will print something like:

POST http://localhost:8081/this/url?id=31912
Host: demo.site
User-Agent: lwp-request/6.31 libwww-perl/6.31
Content-Length: 1300
Content-Type: application/x-www-form-urlencoded
CustomHeader: 

200 File was accepted
Connection: close
Client-Date: Sun, 03 May 2020 20:58:58 GMT
Client-Peer: 127.0.0.1:8081
Client-Response-Num: 1
XXX-Filename: upload.txt

#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <boost/beast/http.hpp>
#include <boost/optional/optional_io.hpp>
...

Followed by the rest of the test.cpp file

You can do a similar request without POST, e.g. using curl:

curl http://127.0.0.1:8081/this/url?id=$RANDOM -H 'Host: demo.site' -d @test.cpp

Upvotes: 3

Related Questions