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.43k stars 889 forks source link

File Parse problem #389

Closed saivamsi98 closed 3 years ago

saivamsi98 commented 3 years ago

I need to read a json file when a GET request comes. How to do that using std::ostringstream os,can anyone help me with this.....?

``

CROW_ROUTE(app, "/getconfig") .methods("GET"_method) ([](const crow::request& req) { return crow::response(200); std::ostringstream os; //need to read json file using os return crow::response{os.str()}; });

app.port(18080) .multithreaded() .run();

``

saivamsi98 commented 3 years ago

I tried this way, when I run this :" curl -X GET http://localhost:18080/getconfig" - It is not displaying the contents in the file and it is throwing an exception. Can anyone explain....?

``

CROW_ROUTE(app, "/getconfig") .methods("GET"_method) ([](const crow::request& /req/, crow::response& res /string filename/) { try{ std::ifstream f("o.json", std::ifstream::binary); std::ostringstream os; f.seekg (0, f.end); long length = f.tellg(); f.seekg (0); char * buffer = new char [length]; f.read (buffer,length); cout << buffer; os << buffer; f.close(); res.write(os.str()); res.end(); } catch(...) { cout << "exception" << endl; } //os << crow::json::load("o.json"); //return crow::response{os.str()}; }); ``

mrozigor commented 3 years ago

May I ask you to use code formatting/displaying code in block? Which exception? It should have message with description of problem. Do you have file o.json in the same directory as app?