Using QHttpMultiPart with QHttpPart Binary and Form Data

1.8k views Asked by At

I wrote a method to send a image and some form data to a Django Rest Framework Server. The following method works fine, but I would like to know if there is a way to send the form fields together in one QHttpPart, instead of creating one for each field?

Is there a better way to do this? Maybe with less code?

void MyNetwork::sendBinaryFile() {

  QNetworkAccessManager *manager = new QNetworkAccessManager(this);
  connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyfinished(QNetworkReply*)));
  QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

  QHttpPart aluno, palavra_chave, latitude, latitude_ref, longitude, longitude_ref;

  aluno.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"aluno\""));
  aluno.setBody("some useful string");

  palavra_chave.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"palavra_chave\""));
  palavra_chave.setBody("some other useful string");

  latitude.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"latitude\""));
  latitude.setBody("0");

  longitude.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"longitude\""));
  longitude.setBody("0");

  latitude_ref.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"latitude_ref\""));
  latitude_ref.setBody("N");

  longitude_ref.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"longitude_ref\""));
  longitude_ref.setBody("E");

  multiPart->append(aluno);
  multiPart->append(palavra_chave);
  multiPart->append(latitude);
  multiPart->append(latitude_ref);
  multiPart->append(longitude);
  multiPart->append(longitude_ref);

  QHttpPart imagePart;
  imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
  imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; filename=\"image.png\"; name=\"file\""));
  QFile *file = new QFile("/path/image.png");
  file->open(QIODevice::ReadOnly);
  imagePart.setBodyDevice(file);
  file->setParent(multiPart); 
  multiPart->append(imagePart);

  QString sendPath = "http://localhost/api/images/";
  QUrl url(sendPath);
  QNetworkRequest request(url);
  QString concatenated = "username:password";
  QByteArray data = "Basic ";
  data.append(concatenated.toLocal8Bit().toBase64());
  request.setRawHeader("Authorization", data);

  manager->post(request,multiPart);
}

void MyNetwork::replyfinished(QNetworkReply* reply)
{
  qDebug() << "C++ - replyUpdateJsonFinished";
  qDebug() << QString(reply->readAll());
  qDebug() << QString(reply->errorString());
}
1

There are 1 answers

1
Kevin Brown-Silva On

Because of the nature of multipart/form-data, each field that is sent must be sent in a different part. This is because each part in the request represents a different field, and is identified by the field name and contains the body that should be sent (in your case, mostly text).

You may have better luck creating a helper method for the text field that takes the field name and the content of it. That method would then be able to create a new part with the correct header (containing the field name) and the body (containing the data to send), without requiring you to repeat the same lines.