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

How to get query parameters? #20

Closed
yoshihitoh opened this issue Oct 25, 2018 · 3 comments
Closed

How to get query parameters? #20

yoshihitoh opened this issue Oct 25, 2018 · 3 comments
Labels
Help Wanted Extra attention is needed Question Further information is requested

Comments

@yoshihitoh
Copy link

Hi there!

I'm interested in to use oat++.
oatpp-examples works fine, really good to know how to use oat++!
Unfortunately, I couldn't find how to get the query parameters from endpoint defined on ApiController.

I saw that Url class has queryParams, so that I think query parameters are available on oat++.
Please let me know how to get that.

Thanks.

@lganzzzo
Copy link
Member

Hello @yoshihitoh !

Thank you for the question. I appreciate your interest in the project!

Here is the example endpoint:

  ENDPOINT("GET", "example/path/{path-param}/query/*", getUserWithQueryParams,
           REQUEST(std::shared_ptr<IncomingRequest>, request), // Map request object to endpoint method
           PATH(String, pathParam, "path-param")) {
    
    /* get url 'tail' - everything that comes after '*' */
    String tail = request->getPathTail(); // everything that goes after '*'
    
    /* check tail for null */
    OATPP_ASSERT_HTTP(tail, Status::CODE_400, "null query-params");
    
    /* parse query params from tail */
    auto queryParams = oatpp::network::Url::Parser::parseQueryParams(tail);
    
    /* get your param by name */
    auto queryParameter = queryParams->get("param"/* parameter name */, "default-value" /* default value */);
    
    /* return result */
    return createResponse(Status::CODE_200, "path-param=" + pathParam + " query-param=" + queryParameter);
    
  }

Currently you have to map REQUEST(std::shared_ptr<IncomingRequest>, request) parameter in to your endpoint. So you can get all params of the request.

Curls:

curl -X GET "http://localhost:8000/example/path/2/query?param=hello-world"
path-param=2 query-param=hello-world

curl -X GET "http://localhost:8000/example/path/2/query/?param=hello-world"
path-param=2 query-param=hello-world

Your request will work in both cases with and without '/' before the '*'. But '/' is required before '*' in the endpoint declaration

Regards,
Leonid

@ghost
Copy link

ghost commented Oct 25, 2018

@lganzzzo Thanks for the detailed explanation!
Your code works perfectly. I didn't realize that PathTail means the query string 😀

Many thanks,
Yoshihito

@lganzzzo
Copy link
Member

You are welcome
I am happy to help!

Best Regards,
Leonid

@lganzzzo lganzzzo added Help Wanted Extra attention is needed Question Further information is requested labels Feb 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted Extra attention is needed Question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants