cpprestsdk icon indicating copy to clipboard operation
cpprestsdk copied to clipboard

Response 404 but request works on curl

Open Victorvhrn opened this issue 6 years ago • 4 comments

I'm trying to make a POST request to the Graphhopper API(https://graphhopper.com) using the code below but the response given is 404. When I use curl to make the request using the output of the req.to_string() method the correct results are given (I need to escape the quotes on the json string though). Am I missing something?

using namespace web;
int total_locals = 4;
std::string* lat_longs = new std::string[4];
lat_longs[0] = "-22.886805,-43.282360";
lat_longs[1] = "-22.886805,-43.282360";
lat_longs[2] = "-22.886805,-43.282360";
lat_longs[3] = "-22.900555,-43.277941";

json::value post_data;
post_data[U("from_points")] = json::value::array();
post_data[U("to_points")] = json::value::array();
double lat,longi;
std::string token;
for(int l1 = 0; l1 < total_locals; ++l1){
    std::istringstream ss(lat_longs[l1]);
    std::getline(ss, token, ','); lat = stod(token);
    std::getline(ss, token, ','); longi = stod(token);
    post_data[U("from_points")][l1] = json::value::array();
    post_data[U("from_points")][l1][0] = json::value::number(longi);
    post_data[U("from_points")][l1][1] = json::value::number(lat);
}
for(int l = 0; l < num_locals; ++l){
    std::istringstream ss(lat_longs[locals[l]]);
    std::getline(ss, token, ',');
    lat = stod(token);
    std::getline(ss, token, ',');
    longi = stod(token);
    post_data[U("to_points")][l] = json::value::array();
    post_data[U("to_points")][l][0] = json::value::number(longi);
    post_data[U("to_points")][l][1] = json::value::number(lat);
}
post_data[U("out_arrays")] = json::value::array();
post_data[U("out_arrays")][0] = json::value(U("times"));
post_data[U("vehicle")] = json::value(U("car"));

http::client::http_client gh(U("https://graphhopper.com/api/1/matrix?key="+GH_KEY));

http::http_request req = http::http_request(http::methods::POST);
req.set_request_uri(gh.base_uri());
req.set_body(post_data);

std::cout << req.to_string() << "\n";

auto response = gh.request(req).get();

json::value resp_data = response.extract_json().get();
std::cout << "Status: " << response.status_code() << "\n";
std::cout << "Response: " << resp_data.serialize() << "";

delete[] lat_longs;

Victorvhrn avatar Jun 19 '19 13:06 Victorvhrn

can you paste the curl command you are using?

Stateford avatar Jun 24 '19 17:06 Stateford

can you paste the curl command you are using?

The command is:

curl -H "Content-Type: application/json" -d "{\"from_points\":[[-43.282359999999997,-22.886804999999999],[-43.282359999999997,-22.886804999999999],[-43.282359999999997,-22.886804999999999],[-43.277940999999998,-22.900555000000001]],\"out_arrays\":[\"times\"],\"to_points\":[[-43.282359999999997,-22.886804999999999],[-43.282359999999997,-22.886804999999999],[-43.282359999999997,-22.886804999999999],[-43.277940999999998,-22.900555000000001]],\"vehicle\":\"small_truck\"}" https://graphhopper.com/api/1/matrix?key=MY_KEY

The output is a JSON, kinda like I expected.

Victorvhrn avatar Jun 25 '19 15:06 Victorvhrn

req.set_request_uri(gh.base_uri());

Don't do that, I suspect.

garethsb avatar Jun 28 '19 08:06 garethsb

@Victorvhrn do you remember what was the issue?

fran203 avatar Oct 13 '21 10:10 fran203