C++ REST API (POST Method) JSON Paylod -- Getting error "No content to map to Object due to end of input"

Sep 24, 2014 at 8:38 PM
Hi , We are getting the following error message while invoking a REST API with a "POST" method . "No content to map to Object due to end of input"

The JSON payload needs to be sent something like this {"value":""abcdefgh"}

The following code is being used . Are we wrong somewhere

auto fileStream = std::make_shared<ostream>();
    pplx::task<void> requestTask = fstream::open_ostream(U("createprocdomain.txt")).then([=](ostream outFile)
        *fileStream = outFile;

        //set credentials
        http_client_config config_cred;
        credentials cred = credentials(U("admin"), U("admin")); // to be parameterized admin/ admin (username/password)

        uri_builder builder(U("")); // to be parametrized 
        // set path
        builder.set_path(U("PrismGateway/services/rest/v1/protection_domains")); // to be parameterized

    __  json::value postData;
        postData[U("value")] = json::value::string(U("abcdefgh"));__

        // create connection
        http_client client(builder.to_string(), config_cred);
        http_request request;
        request.set_method(U("POST")); // POST , DELETE 

        request.headers().add(U("Authorization"), U("Basic YWRtaW46YWRtaW4=")); // to be paramerized

        request.set_body(postData.serialize(), U("application/json"));
    //  request.set_body(utility::stringstream_t("{value:abcdefgh}"));
        return client.request(request);

        .then([=](http_response response)
        printf("Received response status code:%u\n", response.status_code());
        printf("Received response :%u\n", response.body());
        //int i = 

        return response.body().read_to_end(fileStream->streambuf());

        // Close the file stream.
        return fileStream->close();

    // Wait for all the outstanding I/O to complete and handle any exceptions
    // catch (const std::exception &e)
    catch (http_exception &e)
        std::cout << e.what() << " " << e.error_code();
        printf("Error exception:%s\n", e.error_code());

Oct 2, 2014 at 12:50 AM
Hi parthapratimnayak,

I don't completely understand what you are saying. What exactly do you mean when you say you get the error message "No content to map to Object due to end of input"? Is your http server returning an error status code with a string message body containing this message? If so it sounds like you are connecting fine to the server, but it is encountering a problem with your request body or expecting different input data? I'm not sure what platform you are running on but you should use a tool like Fiddler to check that the outgoing HTTP request actually contains all the information necessary.

A miscellaneous note about your code above. There is no need to perform the JSON serialization yourself. The http_request::set_body function has an overload which takes a json::value. In fact it looks like you probably can even just directly use on the http_client::request(...) helper overloads instead of directly constructing an http_request object yourself if you want.