CPPRest SDK making HTTP request to Server

Report
Question

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;
            }
            printJSON(value);
            if(key.size() != 0){
                std::wcout << "End of Parent: " << key.c_str() << std::endl;
            }
        }else{
            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");
request.set_body("u_id=test_admin&pwd=123456789");

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){
            try{
                const json::value & v = previousTask.get();
                printJSON(v);
            } catch(const http_exception &e){
                std::cout<<e.what()<<std::endl;
            }
        });
try{
    task.wait();
} catch(std::exception &e){
    std::cout<<e.what()<<std::endl;
}
}

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 37 views 0

Answer ( 1 )

    0
    January 11, 2017 at 8:30 pm

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

    Report Cancel

    Vague.
    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

Browse

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>