Crow icon indicating copy to clipboard operation
Crow copied to clipboard

Cannot parse query parameters

Open datlt4 opened this issue 3 years ago • 1 comments

I have a GET api, and I my code is:

    CROW_ROUTE(app, "/license").methods("GET"_method)([](const crow::request &req)
                                                        {
        try
        {
            if (req.url_params.get("serial") == nullptr)
                return crow::response(400);
            additionalPayload = std::string(req.url_params.get("serial"));

            if (req.url_params.get("period") == nullptr)
                period = 86400U;
            else
                period = static_cast<unsigned int>(std::stoi(std::string(req.url_params.get("period"))));

            if (req.url_params.get("licensee") == nullptr)
                licensee = "Vizgard_ltd";
            else
                licensee = std::string(req.url_params.get("licensee"));
            std::cout << additionalPayload << "  " << period << "  " << licensee << std::endl;

            return crow::response(200);
        }
        catch (const std::exception &e)
        {
            std::cerr << "[ERROR] " << e.what() << std::endl;
            return crow::response(500);
        } });

Before, I used crow to create my api, but I used this file of ipkn. But now I move to the latest crow_all.h v1.0+4, my API cannot query with old curl command:

curl http://192.168.120.107:6262/license?serial=10932847102398&period=984&licensee=EMoi

API only read the first parameter (like serial in above command), and other parameters were ignored. Does my above code is compatible with the latest crow_all.h v1.0+4?

datlt4 avatar Aug 12 '22 23:08 datlt4

Hi!

I'm running

wget "https://github.com/CrowCpp/Crow/releases/download/v1.0%2B4/crow_all.h"
g++ test.cpp # gcc 11.2 or clang 13.0
./a.out & sleep 1 && curl "http://0.0.0.0:18080/license?serial=10932847102398&period=984&licensee=EMoi" && fg

And its printing (as expected).

10932847102398  984  EMoi

The example code is:

#include "crow_all.h"

int main()
{
    crow::SimpleApp app;

    CROW_ROUTE(app, "/license").methods("GET"_method)([](const crow::request &req) {
        std::string additionalPayload, licensee;
        unsigned int period;
        try
        {
            if (req.url_params.get("serial") == nullptr)
                return crow::response(400);
            additionalPayload = std::string(req.url_params.get("serial"));

            if (req.url_params.get("period") == nullptr)
                period = 86400U;
            else
                period = static_cast<unsigned int>(std::stoi(std::string(req.url_params.get("period"))));

            if (req.url_params.get("licensee") == nullptr)
                licensee = "Vizgard_ltd";
            else
                licensee = std::string(req.url_params.get("licensee"));
            std::cout << additionalPayload << "  " << period << "  " << licensee << std::endl;

            return crow::response(200);
        }
        catch (const std::exception &e)
        {
            std::cerr << "[ERROR] " << e.what() << std::endl;
            return crow::response(500);
        } });

    app.port(18080).run();
    return 0;
}

Such a critical issue seems a bit unlikely to me - but no one ever knows.

  • I see that your variables have a bigger scope than the handler. Are you sure nothing is accessing them?
  • What compiler are you using?

dranikpg avatar Aug 13 '22 12:08 dranikpg

@dranikpg thank you for your reply, it's worth it for me. I think the issue i found related cURL command I used, it seem I need use " character in GET link

datlt4 avatar Aug 14 '22 21:08 datlt4