0rangeFox / Drogon-JWT-Filter-example

A simple project to serve as an example of how to implement an JWT filter on Drogon framework.
MIT License
25 stars 3 forks source link

Getting the json content of the request in the filter #7

Closed UInSomnia closed 8 months ago

UInSomnia commented 8 months ago

Hello!

I'm trying to get json from a request in the filter for further analysis of this data by the filter. But I get a null pointer Is there any way to get json from a request in a filter? Thanks for the answers!

void InSomnia::Filter::Login::doFilter(
    const drogon::HttpRequestPtr &req,
    drogon::FilterCallback &&fcb,
    drogon::FilterChainCallback &&fccb)
{
    const std::shared_ptr < Json::Value > jsonPtr = req->jsonObject();
    if(!jsonPtr)
    {
        // Gets here
    }
}
UInSomnia commented 8 months ago

The problem is solved! The request consists of two parts - Option and Post. Option comes first - it does not contain JSON. That is, I added the following code to the beginning of the filter and everything worked fine

if (req->getMethod() == drogon::HttpMethod::Options)
{
    return fccb();
}