CPPRest SDK making HTTP request to Server


Please briefly explain why you feel this question should be reported .

Report Cancel

during CPPRest SDK (2.8) testing, I initialized an HTTP Request simulating user login to the local server, I am expecting a JSON string to be returned indicating if login succeed. here is the code I wrote.

void printJSON(json::value v)
if (!v.is_null()){
    // Loop over each element in the object
    for (auto iter = v.as_object().cbegin(); iter != v.as_object().cend(); ++iter){
        const string &key = iter->first;
        const json::value &value = iter->second;

        if (value.is_object() || value.is_array()){
            if(key.size() != 0){
                std::wcout << "Parent: " << key.c_str() << std::endl;
            if(key.size() != 0){
                std::wcout << "End of Parent: " << key.c_str() << std::endl;
            std::wcout << "Key: " << key.c_str() << ", Value: " << value.to_string().c_str() << std::endl;

void login(){
http_client client("http://localhost:8080/user");

http_request request(methods::POST);
request.headers().add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
request.headers().add("Content-Length", "100");
request.headers().add("Host", "testhost.com");
request.headers().add("X-Requested-With", "XMLHttpRequest");

pplx::task<void> task = client.request(request)
        .then([](http_response response)-> pplx::task<json::value>{
            if(response.status_code() == status_codes::OK){
                return response.extract_json();
            } else {
                return pplx::task_from_result(json::value());
        .then([](pplx::task<json::value> previousTask){
                const json::value & v = previousTask.get();
            } catch(const http_exception &e){
} catch(std::exception &e){

When I run this code, nothing happened, it seems request never reaches to the Server which has been tested using JSP, So I am pretty sure something went wrong in my code. please help, thanks

solved 0
1 Answer 61 views 0

Answer ( 1 )

    January 11, 2017 at 8:30 pm

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    If you’re saying that the request is not reaching the server, there might be a problem with the listener at the time you executed this code.

    Your request format is correct and running, you may try to wrap the body (u_id, pwd) into a json, and see if it works.

    The bottomline is debugging or sharing your server code would probably help clarify things a bit more.

    Best answer

Leave an answer


What is the capital of Egypt ? ( Cairo )

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>