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

Return json in response of apis ? #263

Closed
Shravan40 opened this issue Nov 15, 2017 · 6 comments
Closed

Return json in response of apis ? #263

Shravan40 opened this issue Nov 15, 2017 · 6 comments

Comments

@Shravan40
Copy link

Shravan40 commented Nov 15, 2017

I am using nlohmann json library in my crow project. Below is my sample POST method.

// Add two numbers
    
    CROW_ROUTE(app,"/add")
            .methods("POST"_method)
    ([](const crow::request& req) {
        auto x = json::parse(req.body);
        if (x == nullptr)
            return crow::response(400);
        auto sum = x["a"].get<long double>() + x["b"].get<long double>();
        bool status = true;
        string message = "It doesn't matter, right ?";
        json return_json = {
            {"status", status},
            {"message", message},
            {"sum", sum},
        };
        return crow::response(return_json);
    });

2nd last line of my code produces error because error: call of overloaded ‘response(json&)’ is ambiguous.

When I replace it with return crow::response(return_json.dump()); it works perfectly, because this converts the json to string type. I would like to know, how can I return the actual json in response of my apis.

@Shravan40 Shravan40 changed the title Not able to return json of any method ? Not able to return json in response of any apis ? Nov 15, 2017
@Shravan40 Shravan40 changed the title Not able to return json in response of any apis ? Return json in response of apis ? Nov 15, 2017
@moneroexamples
Copy link

There are few ways you can do it. Simples is to just dump it as a string: return crow::response(return_json.dump());

You can also define your own respnse:

struct jsonresponse: crow::response
{
    jsonresponse(const nlohmann::json& _body)
            : crow::response {_body.dump()}
    {
        add_header("Access-Control-Allow-Origin", "*");
        add_header("Access-Control-Allow-Headers", "Content-Type");
        add_header("Content-Type", "application/json");
    }
};
}

And then return it.

I'm using the same json library as you. You can have a look how I do it: https://github.com/moneroexamples/onion-monero-blockchain-explorer/blob/master/main.cpp Might find something that will help you.

@Shravan40 Shravan40 reopened this May 17, 2018
@Shravan40
Copy link
Author

Shravan40 commented May 17, 2018

@moneroexamples :I am trying to return json response the way you have suggested. But I am not able to return an object as response. Here is the error I am receiving

static assertion failed: could not find from_json() method in T's namespace

Do you have any idea, how it can be resolved ?

@mrozigor
Copy link

Can you show code sample? And maybe full error text?

@Shravan40
Copy link
Author

Shravan40 commented May 17, 2018

@mrozigor : Sure
Here are the sample block of code

namespace changestreet {
    struct jsonresponse: crow::response {
        jsonresponse(const nlohmann::json& _body): crow::response {_body.dump()} {
            add_header("Access-Control-Allow-Origin", "*");
            add_header("Access-Control-Allow-Headers", "Content-Type");
            add_header("Content-Type", "application/json");
        }
    };
}

class Employer_access_detail {
public:
        size_t user_id = 0;
        string auth_token = "", message;
};

CROW_ROUTE(app,"/v1/login")
        .methods("POST"_method)
([](const crow::request& req) {
        // login do not require any header verification
        json input_json = json::parse(req.body);
        Employer_access_detail login_output = login(input_json);
        changestreet::jsonresponse return_json{login_output};
        return return_json;
});

And Error is coming in the 3rd last line of above code, where I am trying to convert UDT to json.

@mrozigor
Copy link

mrozigor commented May 17, 2018

Based on other responses in nlohmann's JSON library you should implement method 'from_json' outside class:

void from_json(const nlohmann::json& j, Employer_access_detail& d) {
    d.user_id = j.at("user_id");
    d.auth_token = j.at("auth_token");
}

@Shravan40
Copy link
Author

Sadly nlohmann json library doesn't support user defined datatype of json. One can read more about it here

GerHobbelt pushed a commit to GerHobbelt/crow that referenced this issue Nov 12, 2021
Add `get_remote_ip` method to `websocket::connection`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants