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

The server responds 500 if the request body is null #235

Closed
BossZou opened this issue May 6, 2020 · 3 comments
Closed

The server responds 500 if the request body is null #235

BossZou opened this issue May 6, 2020 · 3 comments
Labels
Question Further information is requested

Comments

@BossZou
Copy link
Contributor

BossZou commented May 6, 2020

I compile with oatpp source code on branch master, and define a endpoint as follows:

ENDPOINT("GET", "/collections/{collection_name}/partitions", ShowPartitions, PATH(String,collection_name),
                   QUERIES(const QueryParams&, query_params), BODY_STRING(String, body)) { ... }

And I tried to send a request without any pay-load:

curl 'http://127.0.0.1:19121/collections/demo_partition_collection/partitions'

I received a error:

 $ curl "http://127.0.0.1:19121/collections/demo_partition_collection/partitions" -v
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 19121 (#0)
> GET /collections/demo_partition_collection/partitions HTTP/1.1
> Host: 127.0.0.1:19121
> User-Agent: curl/7.58.0
> Accept: */*
> 
< HTTP/1.1 500 Internal Server Error
< Content-Length: 163
< Server: oatpp/1.0.0
< Connection: close
< 
server=oatpp/1.0.0
code=500
description=Internal Server Error
message=[oatpp::web::protocol::http::incoming::SimpleBodyDecoder::decode()]: Error. Invalid Request.
* Closing connection 0

However, I tried to compile with previous version of oatpp (such as v0.19.11), server work well.

@lganzzzo
Copy link
Member

lganzzzo commented May 6, 2020

Hello @BossZou ,

The usage of BODY_STRING and BODY_DTO may slightly change.
The idea we are trying to follow is:

☝️ All the parameters that are mapped in endpoint are required parameters☝️

Required Body

  ENDPOINT("GET", "/collections/{collection_name}/partitions", ShowPartitions,
           PATH(String, collection_name),         // <--- required parameter!
           QUERIES(QueryParams, query_params),    // <--- can be empty because it's a set of params.
           BODY_STRING(String, body))             // <--- required parameter!
  {
    return createResponse(Status::CODE_200, "OK");
  }

Optional Body

In order to have an "optional body" in endpoint, you can do the following
(this should work at all times):

  ENDPOINT("GET", "/collections/{collection_name}/partitions", ShowPartitions,
           PATH(String, collection_name),
           QUERIES(QueryParams, query_params),
           REQUEST(std::shared_ptr<IncomingRequest>, request))
  {
    try {
      auto body = request->readBodyToString();
      // todo
    } catch (...) {
      // currently readBodyToString() throws std::runtime_error.
      // But it should be, and might be changed to oatpp::web::protocol::http::HttpError
    }
    return createResponse(Status::CODE_200, "OK");
  }

Ignored Body

If you don't want to process the body at all - just remove the BODY_STRING.

  ENDPOINT("GET", "/collections/{collection_name}/partitions", ShowPartitions,
           PATH(String, collection_name),         // <--- required parameter!
           QUERIES(QueryParams, query_params))    // <--- can be empty because it's a set of params.
  {
    return createResponse(Status::CODE_200, "OK");
  }

Regards,
Leonid

@lganzzzo lganzzzo added the Question Further information is requested label May 6, 2020
@lganzzzo lganzzzo changed the title Server fail if request body is null The server responds 500 if the request body is null May 6, 2020
@lganzzzo
Copy link
Member

Hey @BossZou ,

Please note that starting from oatpp 1.1.0
Such syntax is invalid.

QUERIES(const QueryParams&, query_params) //<--- Compilation Error

Use the following instead:

QUERIES(QueryParams, query_params) //<--- const & added automatically

Please find the full changelog here - https://github.com/oatpp/oatpp/blob/master/changelog/1.1.0.md

Regards,
Leonid

@lganzzzo
Copy link
Member

Closing this issue...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants