Response 404 but request works on curl
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;
can you paste the curl command you are using?
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.
req.set_request_uri(gh.base_uri());
Don't do that, I suspect.
@Victorvhrn do you remember what was the issue?