This forum has been archived. All content is frozen. Please use KDE Discuss instead.

[SOLVED] downloader() and POST method

Tags: None
(comma "," separated)
User avatar
Ignacio Serantes
Registered Member
Posts
453
Karma
1
OS
I would like to use the POST method instead of GET with downloader(), is this possible?

If possible an example would be welcome :).

Last edited by Ignacio Serantes on Sun Mar 22, 2009 3:11 pm, edited 1 time in total.


Ignacio Serantes, proud to be a member of KDE forums since 2008-Nov.
User avatar
markey
KDE Developer
Posts
2286
Karma
3
OS

Re: downloader() and POST method

Sun Mar 08, 2009 6:16 pm
I'm not sure that this belongs in "downloader" (kinda illogical), but generally: Why not, we could add it.


--
Mark Kretschmann - Amarok Developer
User avatar
Ignacio Serantes
Registered Member
Posts
453
Karma
1
OS

Re: downloader() and POST method

Sun Mar 08, 2009 6:56 pm
Sorry, my English is not good and I do not understand, what is illogical?

There are many servers that work with POST method and not with GET and there is problems with the size limit of the URL.

Now I need multiple calls to download() to translate a simple song on Google Translation Service because I can not use the POST method.

Unfortunately for me, very useful servers works only with POST method and I can not do the same dirty trick.


Ignacio Serantes, proud to be a member of KDE forums since 2008-Nov.
User avatar
Ignacio Serantes
Registered Member
Posts
453
Karma
1
OS
If someone needs to use the POST method here as I've solved the problem myself requesting data to Google Translation Service.

Sorry that the text is not formatted but I tried the toolbar with Arora, Konqueror and Firefox and have not managed to make me work. Is only Internet Explorer compatible?

UPDATE: a new and improved method :). You can copy and paste in "Amarok Script Console" directly to test it.

-< code >--< code >--< code >--< code >--< code >-
Importer.loadQtBinding("qt.network");

function onDownloadTranslate() {
  Amarok.alert(Amarok.Lyrics.toUtf8(http.readAll()));
} // function onDownloadTranslate()

var langOrig = 'auto';
var langDest = 'en';

data = 'Esto es una prueba de uso del método POST';
http = new QHttp();
http.done.connect(onDownloadTranslate);
http.setHost('www.google.com');
http.post('/translate_a/t?client=t&sl=' + langOrig + '&tl=' + langDest, Amarok.Lyrics.fromUtf8('hl=en&oe=UTF-8&ie=UTF-8&text=' + data, 'UTF-8'));
-< code >--< code >--< code >--< code >--< code >-

OLD method
-< code >--< code >--< code >--< code >--< code >-
Importer.loadQtBinding("qt.network");

function onDownloadTranslate() {

    var data = Amarok.Lyrics.toUtf8(http.readAll());

} // function onDownloadTranslate

function translate(data) {
    try {

      http = new QHttp();
      http.done.connect(onDownloadTranslate);

      var langOrig = 'auto';
      var langDest = 'es';

      var header = new Array('www.google.com/translate_a/t?client=t&sl=' + langOrig + '&tl=' + langDest, new Array('Host', 'www.google.com'), new Array('User-Agent', 'Mozilla/5.0'), new Array('Accept-Encoding', 'text/html,text/plain'));

      var url = header[0];
      var hrh = new QHttpRequestHeader('POST', url, 1, 1);
      var host = url.substring(0, url.indexOf('/'));
      for (idx = 1; idx < header.length; idx++) {
        hrh.setValue(header[idx][0], header[idx][1]);
      }
      hrh.setContentLength(data.length);
      hrh.setValue('Connection', 'Close');

      http.setHost(host);
      http.request(hrh, Amarok.Lyrics.fromUtf8('text=' + data, 'UTF-8'));

    } catch(err) {
          Amarok.debug('error: ' + err);
    }

} // function translate

var http = null;
-< code >--< code >--< code >--< code >--< code >-

Last edited by Ignacio Serantes on Sat Mar 28, 2009 10:10 am, edited 1 time in total.


Ignacio Serantes, proud to be a member of KDE forums since 2008-Nov.


Bookmarks



Who is online

Registered users: bancha, Bing [Bot], daret, Evergrowing, Google [Bot], lockheed, sandyvee, Sogou [Bot]