We have moved to a new Sailfish OS Forum. Please start new discussions there.

QML Image.source authentication

asked 2014-02-13 21:23:39 +0300

Mariusmssj gravatar image

updated 2014-02-13 21:27:43 +0300

I am trying to get an image from a web-page, but to access it it requires user authentication. To get xml information I use XMLHttpRequest():

    var httpReq = new XMLHttpRequest()
    var url = "http://" + ip + ":" + port + "/requests/status.xml";

    httpReq.open("GET", url, true);

    // Send the proper header information along with the request
    httpReq.setRequestHeader("Authorization", "Basic " + Qt.btoa(username + ":" + password));

but that only works for XML data, I can access the image by using:

art.source = "http://" + username + ":" + password + "@" + ip + ":" + port + "/art?item=" ;

The problem is that password is passed as plain text.

How would I go make it work without passing the password as plain text?

edit retag flag offensive close delete

1 Answer

Sort by » oldest newest most voted

answered 2014-02-13 21:40:56 +0300

mholo65 gravatar image

Password will always be passed as plain text unless the webserver accepts SSL connections (https). If you are concerned about displaying password in QML file, then you should get the image content in code (c++). This can be done with QNetworkAccessManager.

edit flag offensive delete publish link more


Is it possible to get the image back using the XMLHttpRequest?

Mariusmssj ( 2014-02-13 21:46:45 +0300 )edit

Unfortuneately QML Image.Source does only allow URL as parameter (not binary data as you could get with XmlHttpRequest). Parameter could be either local (from filesystem or resource) or external i.e. from webserver.

Easiest is probably to write a custom QQuickImageProvider (http://qt-project.org/doc/qt-5/qquickimageprovider.html) and get the image from there using QNetworkAccessManager (http://qt-project.org/doc/qt-5/qnetworkaccessmanager.html).

mholo65 ( 2014-02-13 22:28:32 +0300 )edit

Hello again. You should be able to set binary data as Image.Source. Check this out http://stackoverflow.com/questions/6747434/how-can-i-convert-binary-codes-to-png-image-in-qml

First of course you should get the binary data of the image via XmlHttpRequest. This could be helpful: http://stackoverflow.com/questions/7255719/downloading-binary-data-using-xmlhttprequest-without-overridemimetype

But again, the original question: "How would I go make it work without passing the password as plain text?"

Placing the username and password in the Request header exposes the password as much as placing the username and password before the Request URL (and it should be the completely same thing, ie. username and password are added to Request header). The only way to get around this is to use SSL (https).

mholo65 ( 2014-02-13 22:35:33 +0300 )edit
Login/Signup to Answer

Question tools



Asked: 2014-02-13 21:23:39 +0300

Seen: 1,137 times

Last updated: Feb 13 '14