Rest SDK-发布请求404

时间:2019-06-18 20:18:43

标签: c++ json rest sdk

我正在尝试使用Rest SDK(https://graphhopper.com)向Graphhopper API(https://github.com/microsoft/cpprestsdk)发出POST请求,但给出的响应为404。我相信设置时我缺少一些细节我的post_data。 json基本上是两个坐标数组和一些其他字段。代码是这样的:

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;

当我使用curl使用req.to_string()方法的输出来发出请求时,给出了正确的结果(尽管我需要对json字符串的引号进行转义)。我尝试使用序列化的post_data设置请求的正文,并将内容类型指定为application / json,但是得到了相同的404代码。还有其他步骤可以使这项工作吗?

0 个答案:

没有答案