ipkn / crow

Crow is very fast and easy to use C++ micro web framework (inspired by Python Flask)
BSD 3-Clause "New" or "Revised" License
7.48k stars 891 forks source link

Documentation / Post example #288

Open marosiak opened 6 years ago

marosiak commented 6 years ago

Hey, could I get full documentation or post example? I want to recive Post's like http:/example.com/login/?username=tester&password=myPassword But I don't understand the only example which I've found in readme.md

Or mayby any idea how url would looks like for this:

CROW_ROUTE(app, "/add_json")
.methods("POST"_method)
([](const crow::request& req){
    auto x = crow::json::load(req.body);
    if (!x)
        return crow::response(400);
    int sum = x["a"].i()+x["b"].i();
    std::ostringstream os;
    os << sum;
    return crow::response{os.str()};
});
netromdk commented 6 years ago

In this case the request (HTTP) body is expected to be JSON data.

Example

{"a": 1, "b": 2}

If you want query values, you have to access crow::request::url_params (crow/http_request.h:29) on which these are some of the ways you can interact with it (from crow/query_string.h):

char* query_string::get(const std::string& name) const;
std::vector<char*> query_string::get_list(const std::string& name) const;
std::unordered_map<std::string, std::string> query_string::get_dict(const std::string& name) const;

In particular, if you wanted to get "username" and "password":

const auto username = req.url_params.get("username");
const auto password = req.url_params.get("password");

I hope that helps. Feel free to look through the code, it helps a lot to understand how to use it.