eidheim / Simple-Web-Server

A very simple, fast, multithreaded, platform independent HTTP and HTTPS server and client library implemented using C++11 and Boost.Asio. Created to be an easy way to make REST resources available from C++ applications.
MIT License
2.61k stars 751 forks source link

Response get Base64 encoded String fails #136

Closed deshan closed 7 years ago

deshan commented 7 years ago

Hi,

From my java client I want to send a image to server. What I am doing is convert image to bytes and then do base64 encoding and send with http post request.

At the server I am trying to get the response, do base64 decode and create the image again. But when I do base64 decoding it fails and return nothing. So I am wonder what I am doing is wrong. The encoded base64 string not ok with http response?

Here is my code

mServer.resource["^/string$"]["POST"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
  //Retrieve string:
  auto content = request->content.string();
  //request->content.string() is a convenience function for:
  //stringstream ss;
  //ss << request->content.rdbuf();
  //auto content=ss.str();

  std::vector<std::string> params = split(content, '=');

  std::vector<BYTE> decodedData  = base64_decode(params.at(3));

  int size = decodedData.size(); // 0 here`

Thanks Deshan

edit (eidheim): made it more readable

eidheim commented 7 years ago

Try to do the following:

mServer.resource["^/string$"]["POST"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
  auto decoded = SimpleWeb::Crypto::Base64::decode(request->content.string());
}
eidheim commented 7 years ago

But it depends on how the request look. Feel free to post an example request here, as it then would be easier to help you.

eidheim commented 7 years ago

You can also use SimpleWeb::QueryString::parse on the content if the content is a query string, that is regular form post.

deshan commented 7 years ago

If I do string encode it nicely decode at the server. The issue is with image byte encode. Not sure about the way parse at the server. Will try your solutions and update here. Thanks you very much for the quick response.

deshan commented 7 years ago

Still no success, the base54 encoded image byte I send doensn't decode but if it's a string then it works.

Instead of java client I would try the sample c++ client provided to send the image. Does some one tried this before for Sending Image from client and read back in the server?

Do I have to use multipart? What are the modification I have to do to read multipart?

Thanks deshan

eidheim commented 7 years ago

Not sure why Base64 is used as it requires more bytes than when sending binary data, but disregarding that, it would help a lot if you posted an example request sent from the client. That is, how does the header look like, and how does the content look like.