1

I compared the data i send and recieve and it is turned out that the end of my data, which i recieve on my server is cut, is QNetworkmanager has a request lenght limit?

photo struct.

typedef struct Photo_{
    std::string name;
    int likes;
    int comments;
    int id;
    int user_id;
QByteArray img_in_bytes;
}photo;

Here is the function that convert data to json and send it to server.

void MainWindow::send_new_photo_to_server(photo item){
    QJsonDocument doc;

    QNetworkRequest req(url);

    qDebug() << QString::number(item.likes);
    qDebug() << QString::number(item.comments);
    qDebug() << QString::fromStdString(item.name);

    req.setHeader(QNetworkRequest::ContentTypeHeader, "text");
    req.setRawHeader("request-type", "addMainEntity");

    QBuffer buf(&item.img_in_bytes);
    QByteArray img_in_bytes = buf.data().toBase64();

    QJsonObject object;

    object.insert("comments", QString::number(item.comments));
    object.insert("img_in_bytes",QLatin1String(img_in_bytes));
    object.insert("name", QString::fromStdString(item.name));
    object.insert("likes", QString::number(item.likes));

    doc.setObject(object);

    qDebug() << doc.toJson();

    manager->post(req, doc.toJson());

    qDebug() << "here you are";
}

There is function that handle a request. It`s no need in upload procces function becouse data has already been currupted by the time it is called.

void Server::get_request(){

    i++;
    qDebug() << i << "\n";

    QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());

    QString data = socket->readAll();

    socket->waitForBytesWritten(3000);
    qDebug() << "Recieved data: \n" << data;

    this->process_request(data);
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ShyEnough
  • 21
  • 4

1 Answers1

1

I managed to solve my problem but it will work only for transferring data in json. Here is the loop which read all chunks of the data.

Data is a QByteArray which contains data send by client. As you know Json ends with '}' , so i read data from socket until i get the the last chunk of data with '}' in the end.

I know it is very bad practice but it is the only thing that worked out.

Write me back if you find better solution.

Data =  "";
while(!Data.contains("}")){
    socket->waitForReadyRead();
    Data += socket->readAll();
}
ShyEnough
  • 21
  • 4