PDA

View Full Version : Whats the problem with my code (http post)



newtolinux
10th November 2010, 13:42
Hi..i am developing a simple application which uploads image to yfrog.com.(These images will be reflected in twitter account). Here is my code. but it is not working. I am not getting response from server.

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkRequest request(QUrl("http://yfrog.com/api/uploadAndPost"));

QByteArray data;
QUrl params,params1;
QFile file("some image path");
QString boundary("-----abcde12345");
QString body = "\r\n--" + boundary + "\r\n";

params.addQueryItem("username",twitterusername);
params.addQueryItem("password",twitterpassword);
params.addQueryItem("message",some message...);
params.addQueryItem("key",mydeveloperkey);


data.append(body);
data.append(params.toString());
QByteArray ba;
ba=file.readAll();
QString body1(ba);
params1.addQueryItem("media",body1);
data.append(params1.toString());
data.append(body);


request.setRawHeader("Content-Type","multipart/form-data; boundary=-----abcde12345");
request.setHeader(QNetworkRequest::ContentLengthHe ader,data.size());

QNetworkReply *reply = manager->post(request,data);

reply->waitForReadyRead(-1);
qDebug() << "replay :"<<reply->readAll();



If i checked the requested TCP packets from wireshark, it is giving a error message like 'malformed packets'.

For reference : http://code.google.com/p/imageshackapi/wiki/YFROGuploadAndPost


Please any body help regarding this. Where i am doing wrong?

squidge
10th November 2010, 13:52
So what is the difference between the packets sent by your application and the packets sent by a web browser?

You can confirm this using wireshark, as you have used already.

If you are getting an error, please post a screenshot of that error.

Are you correctly mime encoding the image data to a format which the web site supports?

ChrisW67
10th November 2010, 23:47
You are missing [code] tags around your code.

The boundary value almost certainly needs to be quoted in the multipart/form-data content type.
The params probably should be extracted with QUrl::encodedQuery() rather than toString().

Do you really need multipart/form-data if there is only one part?