Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server logging does not show body with a POST #367

Closed
dkeeney opened this issue Feb 27, 2020 · 2 comments
Closed

Server logging does not show body with a POST #367

dkeeney opened this issue Feb 27, 2020 · 2 comments

Comments

@dkeeney
Copy link

dkeeney commented Feb 27, 2020

Refer to Issue #366 You can use this client/server pair to see this problem.

On the server side, the logging facility is passed a 'req' argument. For a POST message the req.body is always empty while printing the log even though a body was received and processed by the server.

I tried this with version 0.5.5 and with version 0.5.6

@yhirose
Copy link
Owner

yhirose commented Feb 27, 2020

@dkeeney, I looked into the post method in your server example in #366.

  svr.Post("/network", [&](const Request &req, Response &res, const ContentReader &content_reader) {
      content_reader([&](const char *data, size_t data_length) {
        res.body.append(data, data_length);
        return true;
      });
    // here is where I would normally create the resource.
    std::cout << "Creating resource /network with configuration: " << res.body << std::endl;
    std::string token = "0001";
    res.set_content(token+"\n", "text/plain");
  });

res.body.append(data, data_length); doesn't look correct. If you want to receive the JSON content in req.body, you can simply do as below.

  svr.Post("/network", [&](const Request &req, Response &res) {
    // here is where I would normally create the resource.
    std::cout << "Creating resource /network with configuration: " << res.body << std::endl;
    std::string token = "0001";
    res.set_content(token+"\n", "text/plain");
  });

Here is the result:

Creating resource /network with configuration:
================================
POST HTTP/1.1 /network
Accept: */*
Connection: close
Content-Length: 502
Content-Type: application/json
Host: localhost:8050
REMOTE_ADDR: 127.0.0.1
User-Agent: cpp-httplib/0.5
Body:
   {network: [
       {addRegion: {name: "encoder", type: "RDSERegion", params: {size: 1000, sparsity: 0.2, radius: 0.03, seed: 2019, noise: 0.01}}},
       {addRegion: {name: "sp", type: "SPRegion", params: {columnCount: 2048, globalInhibition: true}}},
       {addRegion: {name: "tm", type: "TMRegion", params: {cellsPerColumn: 8, orColumnOutputs: true}}},
       {addLink:   {src: "encoder.encoded", dest: "sp.bottomUpIn"}},
       {addLink:   {src: "sp.bottomUpOut", dest: "tm.bottomUpIn"}}
    ]}--------------------------------
200 HTTP/1.1
Connection: close
Content-Length: 5
Content-Type: text/plain

Body: 0001

Is it what you expected? Please let me know if I misunderstood your situation.

By the way, If we use the POST API with Content Receiver, cpp-httplib doesn't use req.body, and it should be empty. It's a user's responsibility to read and store the context somewhere in memory or file. Here is the README example:

svr.Post("/content_receiver",
  [&](const Request &req, Response &res, const ContentReader &content_reader) {
    if (req.is_multipart_form_data()) {
      MultipartFormDataItems files;
      content_reader(
        [&](const MultipartFormData &file) {
          files.push_back(file);
          return true;
        },
        [&](const char *data, size_t data_length) {
          files.back().content.append(data, data_length);
          return true;
        });
    } else {
      std::string body;
      content_reader([&](const char *data, size_t data_length) {
        body.append(data, data_length);
        return true;
      });
      res.set_content(body, "text/plain");
    }
  });

Hope it helps.

@dkeeney
Copy link
Author

dkeeney commented Feb 28, 2020

Yes!!! That was it. When I removed the ContentReader I now have the body in the logs.
I was guessing what the ContentReader was for and I guessed wrong.
Wonderful.
So I will close this Issue.

@dkeeney dkeeney closed this as completed Feb 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants