0

I need to make another HTTP request based on the response of the first request.

this is how I would have done it in Python:

response = request.get("somewhere.com")
response = response.json()

response2 = request.get(f"somewhere.com&query={response[0]}")

in C++ Curl for People would be something like this. But I am unsure:


#include <cpr/cpr.h>
#include <nlohmann/json.hpp>
#include <iostream>

using namespace std;
using json = nlohmann::json;

json jsonify(std::string response)
{
    auto j = json::parse(response);
    json object = j;
    return object;
}

int main()
{
    cpr::Response response = cpr::Get(cpr::Url{"somewhere.com"});
    json res(response.text);
    
    cpr::Response response = cpr::Get(cpr::Url{"somewhere.com&query={res["data"]}"});
}

how do I build a formatted URL in C++?

flx_cod
  • 9
  • 2
  • [`std::format`](https://en.cppreference.com/w/cpp/utility/format/format) (or [the format library](https://github.com/fmtlib/fmt) the standard is based on)? Or otherwise perhaps [`std::ostringstream`](https://en.cppreference.com/w/cpp/io/basic_ostringstream)? Of if all data are strings (more specifically `std::string`) then just the plain `+` operator? As in `"somewhere.com"query="s + some_data`". – Some programmer dude May 21 '22 at 07:23
  • Are you just trying to compose a string? You could just use concatenation... i.e. operator +. As the communication overhead will be the performance bottleneck. Don't complicate things. – JHBonarius May 21 '22 at 07:25
  • I am not trying to contruct a string, I am trying to construct a cpr::Url{} simply trying to concatenate a string and then passing it into the the curly braces in cpr::Url{} isn't working for me – flx_cod May 21 '22 at 07:44
  • To nitpick: You *are* trying to construct a string, a string that you can pass to the `cpr::Url` constructor. If you want to learn C++ you have to learn it properly, you can't use a different language with different constructs as base, and then just guess and hope for the best. Please invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to learn more. – Some programmer dude May 21 '22 at 07:48
  • Thank you for the suggestion, I did get some books and I also learn as I am trying to solve a specific problem. I absolutely do not disagree with you. Using JHBonarius's suggestion, that did solve the problem using the stringstream method. – flx_cod May 21 '22 at 08:24

0 Answers0