Qt or libcurl c++: Facebook api POST request to image upload
<p>Good day. I can't understend how create a POST request to upload an image. I was searching in the net, and found several versions. But is always response for me (after request) - "(# 324) Requires upload file".
Thank you! </p>

<pre><code>ui-&gt;webView-&gt;load(QUrl("https://www.facebook.com/dialog/oauth?"
                       "client_id=XXXXXXXXXX&amp;"
                       "display=popup&amp;"
                       "response_type=token&amp;"
                       "scope=user_status,user_photos,publish_actions&amp;"
                       "redirect_uri=https://www.facebook.com/connect/login_success.html"));
</code></pre>

<p><strong>1</strong></p>

<pre><code>QString uploadUrl = "https://graph.facebook.com/me/photos?access_token=" +
                    accessToken;

QString photoPath = QCoreApplication::applicationDirPath() + "/image_example.png";

QFileInfo fileInfo(photoPath);
QFile file(photoPath);

QString comment = "User provided message";

QString bound="---------------------------17673466415141";

QByteArray data;

data.append("--" + bound + "\r\n");
data.append("Content-Disposition: form-data; name=\"access_token\"\r\n\r\n");
data.append(accessToken + "\r\n");

data.append("--" + bound + "\r\n");
data.append("Content-Disposition: form-data; name=\"source\"; filename=\""
            + file.fileName()+"\"\r\n");
data.append("Content-Type: image/" + fileInfo.suffix().toLower() + "\r\n\r\n");
data.append(file.readAll());
data.append("\r\n");

data.append("--" + bound + "\r\n");
data.append("Content-Disposition: form-data; name=\"message\"\r\n\r\n");
data.append(comment);
data.append("--" + bound + "\r\n");

QNetworkRequest request(uploadUrl);

request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
request.setRawHeader("Accept-Language", "en-us,en;q=0.5");
request.setRawHeader("Accept-Encoding", "gzip,deflate");
request.setRawHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
request.setRawHeader("Keep-Alive", "300");

request.setRawHeader("Content-Type",
                     QString("multipart/form-data; boundary=" + bound).toLatin1());
request.setRawHeader("Content-Length", QString(data.size()).toLatin1());

QNetworkAccessManager * manager = new QNetworkAccessManager(this);
QNetworkReply * reply =  manager-&gt;post(request, data);
QEventLoop wait;

connect(manager, SIGNAL(finished(QNetworkReply*)), &amp;wait, SLOT(quit()));
connect(manager, SIGNAL(finished(QNetworkReply*)), manager, SLOT(deleteLater()));

QTimer::singleShot(10000, &amp;wait, SLOT(quit()));
wait.exec();

QByteArray answer = reply-&gt;readAll();
reply-&gt;deleteLater();

qDebug() &lt;&lt; answer;
</code></pre>

<p><strong>2</strong></p>

<pre><code>QString uploadUrl = "https://graph.facebook.com/me/photos?access_token=" +
                    accessToken;

QString photoPath = QCoreApplication::applicationDirPath() + "/image_example.png";

QFileInfo fileInfo(photoPath);
QFile file(photoPath);

QPixmap pixmap;
pixmap.load(photoPath);
QByteArray data;

QBuffer buffer(&amp;data);
buffer.open(QIODevice::ReadWrite);
pixmap.save(&amp;buffer, "png");
buffer.close();

QHttpMultiPart * multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));

imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"source\"; filename=\""+ file.fileName()));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"message\""));

imagePart.setBody(data);
multiPart-&gt;append(imagePart);

QNetworkAccessManager * manager = new QNetworkAccessManager(this);
QNetworkReply * reply =  manager-&gt;post(request, multiPart);
QEventLoop wait;

connect(manager, SIGNAL(finished(QNetworkReply*)), &amp;wait, SLOT(quit()));
connect(manager, SIGNAL(finished(QNetworkReply*)), manager, SLOT(deleteLater()));

QTimer::singleShot(10000, &amp;wait, SLOT(quit()));
wait.exec();

QByteArray answer = reply-&gt;readAll();
reply-&gt;deleteLater();

qDebug() &lt;&lt; answer;
</code></pre>