Search code examples
qthttpmultipartqnetworkaccessmanager

Qt multipart post problem


I need help with sending multipart post request. Main problem is that server doesn't recognise my request's post body part. Server side is working fine with android, but not with Qt and NetworkAccessManager.

Here is the code:

Here I make bodyPart to send with request (transferData is a QByteArray with some text):

postBody.append("\r\n--"+ boundary + "\r\n");
postBody.append("Content-Disposition: form-data; name=\"auth\" \r\n");
postBody.append(" \r\n");
postBody.append(auth + " \r\n");
postBody.append("--"+ boundary + "\r\n");
postBody.append("Content-Disposition: form-data; name=\"upload-test-data\" \r\n");
postBody.append(" \r\n");
postBody.append(transferData + " \r\n");
postBody.append("--"+ boundary + "\r\n");
postBody.append("Content-Disposition: form-data; name=\"upload-checksum\" \r\n");
postBody.append(" \r\n");
postBody.append(checksum.toHex() + " \r\n");
postBody.append("\r\n--" + boundary + "--\r\n");

Here is headers:

request.setRawHeader("User-Agent", "MyApp 1.0");
request.setRawHeader("Content-Type", "multipart/form-data; boundary="+boundary.toAscii());
request.setHeader(QNetworkRequest::ContentLengthHeader, QString::number(postBody.size()).toAscii());

Solution

  • Try adding "Content-Length" header after setting "Content-Type". Value should be something like QString::number(postBody.length()). (Edited: This didn't fix the problem).

    Removing the extra space before the new line (\r\n) worked. Kindly change postBody.append(" \r\n") to postBody.append("\r\n")