-
Notifications
You must be signed in to change notification settings - Fork 0
How to fetch using CPR and CROW(for json parser)
Debraj Das edited this page Mar 6, 2026
·
1 revision
- setup the crow and cpr library in globally in system
#include <crow.h>
#include <bits/stdc++.h>
#include <cpr/cpr.h>
void solve(std::string url) {
cpr::Response r = cpr::Get(cpr::Url{url});
if (r.status_code != crow::status::ACCEPTED)
return;
std::cout << "Status code: " << r.status_code << '\n'; // e.g., 200
std::cout << "Response: " << r.text << '\n'; // The JSON payload
crow::json::rvalue parsed_data = crow::json::load(r.text);
// 4. Extract the JSON fields directly into standard C++ variables!
std::string user_id = parsed_data["category"].s();
// 5. Now you can use them like normal C++ variables
std::cout << "=== Task Details ===\n";
std::cout << "User ID: " << user_id << '\n';
crow::json::wvalue res;
res["value"] = 10;
res["key"] = "something";
std::cout<<"dump : "<<res.dump()<<std::endl;
}
int main() {
// Fetch data from a public API
solve("https://v2.jokeapi.dev/joke/Programming");
return 0;
}