title: Using QHttpMultiPart with QHttpPart Binary and Form Data
body: <p>I wrote a method to send a image and some form data to a <strong>Django Rest Framework Server</strong>. The following method works fine, but I would like to know if there is a way to send the form fields together in one <strong>QHttpPart</strong>, instead of creating one for each field?</p>
  
  <p>Is there a better way to do this? Maybe with less code?</p>
  
  <pre><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-&gt;append(aluno);
    multiPart-&gt;append(palavra_chave);
    multiPart-&gt;append(latitude);
    multiPart-&gt;append(latitude_ref);
    multiPart-&gt;append(longitude);
    multiPart-&gt;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-&gt;open(QIODevice::ReadOnly);
    imagePart.setBodyDevice(file);
    file-&gt;setParent(multiPart); 
    multiPart-&gt;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-&gt;post(request,multiPart);
  }
  
  void MyNetwork::replyfinished(QNetworkReply* reply)
  {
    qDebug() &lt;&lt; "C++ - replyUpdateJsonFinished";
    qDebug() &lt;&lt; QString(reply-&gt;readAll());
    qDebug() &lt;&lt; QString(reply-&gt;errorString());
  }
  </code></pre>