Unknown error when sending post request with json body using Poco [C++]

anwarjutt 11 Reputation points
2021-06-23T18:34:11.297+00:00

i tried a lot to send post request with json body to firestore rest api using Poco external libraries for c++.

the get request is working probably but the post request dont give a response.

here is my function for post request:

void Connection::post(Poco::JSON::Object::Ptr body){
    // Setup Connection
    string path{uri->getPathAndQuery()};
    if(path.empty()) path = "/";
    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, path, Poco::Net::HTTPMessage::HTTP_1_1);
    request.setContentType(def_request_content_type);
    request.setKeepAlive(true);
    request.setContentLength(500);

    // Finish Connection
    std::ostream &os = session->sendRequest(request);
    body->stringify(os);
    Poco::Net::HTTPResponse response;
    std::stringstream ss;
    std::istream& is = session->receiveResponse(response);
    Poco::StreamCopier::copyStream(is,ss);
    std::cout << ss.str(); }

and this is the function which creates json obeject:

void Connection::send_arguments(const string message,const int index){
  Poco::JSON::Object::Ptr post_body = new Poco::JSON::Object;
  Poco::JSON::Object vars;
  Poco::JSON::Object var_types;

  var_types.set("stringValue", message);
  vars.set("message", var_types);
  var_types.clear();

  var_types.set("integerValue", index);
  vars.set("index", var_types);
  var_types.clear();  

  var_types.set("stringValue", def_messager);
  vars.set("messager", var_types);
  var_types.clear(); 

  post_body->set("fields", vars);
  post_body->stringify(std::cout);
  post(post_body);
}

i hope someone can help me.

The error is the program stopped working and after a while i got Poco::TimeoutException exception.

my firestore api url: https://firestore.googleapis.com/v1/projects/[my project id]/databases/(default)/documents/messages?key=[my web api key];

request content type = application/json

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,563 questions
{count} vote