Results 1 to 16 of 16

Thread: How to code a (simple ?) cUrl command in Qt ?

  1. #1
    Join Date
    Jun 2010
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default How to code a (simple ?) cUrl command in Qt ?

    Hello !
    I have a simple cUrl command that i need to code in a Qt app:
    curl -u user:pass -F "filename=test.txt" -F "fname=@foo.txt" http://www.myserver.com/fileupload.htm

    (Not that i know much of cUrl other then it can do MUCH more :-) )

    So this should fill out a form with a "test.txt" and upload a file while providing basic user/pass credentials.

    Could i do this whith built-in Qt functions or do i need to run the cUrl command line binary OR to use the cUrl library ?

    Any help is really appreciated :-)

    Greetings,

    Nils

  2. #2
    Join Date
    Jun 2010
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    Any help ?
    It seems IF i code the curl command line from above in C++ code using libcurl it wuld be something like this (for the two -F parameters):


    curl_formadd(&formpost,
    &lastptr,
    CURLFORM_COPYNAME, "sendfile",
    CURLFORM_FILE, "postit2.c",
    CURLFORM_END);

    /* Fill in the filename field */
    curl_formadd(&formpost,
    &lastptr,
    CURLFORM_COPYNAME, "filename",
    CURLFORM_COPYCONTENTS, "postit2.c",
    CURLFORM_END);


    But i'd like to avoid to "yet use another lib" if i could do this with Qt native stuff.
    Do you have any hint to do this without curl CLI or libcurl ?

    Nils

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    What kind of help do you expect from us?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jun 2010
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    Hi !
    Sorry if i failed to make myself clear.
    I need some hint / help / sample code that only uses Qt native commands and that has the same result as the curl command line that i posted. (Or presumably as the sniplet of using curl.h).

    I fail to explain it more simple.

    Greetings,

    Nils

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    Hmmm... so you want a complete solution for the task? It's not 5 lines of code, you know...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jun 2010
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    Hi wysota !
    Its not about what i want but what someone is willing to do.
    At least i know that it is NOT 5 lines of code :-)

    I am really not new to Qt but i NEVER did web stuff...
    I assume that the QNetworkAccessManager may be the thing to check out but it looks so complex that a simple "No" or "Yes" would already help. And if someone feels like it i am sure that there are some key aspects that could help me tremendously.
    But with your reputation here i already feel sheepish to explain to YOU you help can look.
    I'll take what i get, networkwise i really start at ZERO.
    And if you tell me not to do it as libcurl is much better to do it then thats it.
    (provided that you give a minimum of reasoning, i would HATE to incorporate another huge lib if i do not need to).

    Greetings,

    Nils

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    Quote Originally Posted by nils_heidorn View Post
    I assume that the QNetworkAccessManager may be the thing to check out but it looks so complex that a simple "No" or "Yes" would already help.
    Yes

    And if someone feels like it i am sure that there are some key aspects that could help me tremendously.
    The key aspect is definitely encoding the form. The rest is easily handled by QNetworkRequest.

    And if you tell me not to do it as libcurl is much better to do it then thats it.
    Well... if you like reinventing the wheel then that's fine with me If size is an issue and you're using Qt anyway then indeed not using 3rd party libs is an advantage.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    nils_heidorn (30th August 2013)

  9. #8
    Join Date
    Jun 2010
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    Hi !

    Thanks again :-)
    Ooookay, if you feel like it, may help me out with encoding the form, right now i do not even know what that may means (well besides foggy ideas) :-)

    And for re-inventing the wheel:
    Of course if it is too hard doing it the "Qt way" then i will use curl, but i hoped that the curl 30 liner could be done at least with a Qt 50 liner or so...

    Its not about reinventing the wheel but about having to get a spare tire and learning that there already is one hidden deep in the trunk that is a BIT hard to get at ;-)


    Greetings,

    Nils

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    Quote Originally Posted by nils_heidorn View Post
    Ooookay, if you feel like it, may help me out with encoding the form, right now i do not even know what that may means (well besides foggy ideas) :-)
    Search the web for "http form encoding"
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    Join Date
    Jun 2010
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    Hi !
    After i knew what to search for i found at least 2 threads that might help me. Now i have to wait for the test server to be up and running to try out stuff :-)

    AM i allowed to post links to other forums here ?
    I'd love too, so others can follow the same path ...

    Just let me know.

    Greetings,

    Nils

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    Quote Originally Posted by nils_heidorn View Post
    AM i allowed to post links to other forums here ?
    I'd love too, so others can follow the same path ...
    Sure, no problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #12
    Join Date
    Jun 2010
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    Hi !

    So here are the links to posts in other forums. I HOPE they will help me, cannot try right now as the test server is not yet up.
    Please let me know if you do *not* think that those links will be helpful.

    http://qt-project.org/forums/viewthread/11361
    http://stackoverflow.com/questions/2...kaccessmanager


    Greetings,
    Nils

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    Basically you should just use QHttpPart class. Just be aware it still requires that you know what you are doing.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #14
    Join Date
    Jun 2010
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    Hi wysota,
    i feared you would say that... I am limited to Qt 4.7.4 and QHttpPart is 4.8...
    Argh#&%$§#

    ;-)

  16. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    It doesn't change that much, you still have to know how to craft the form part either way.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #16
    Join Date
    Jun 2010
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to code a (simple ?) cUrl command in Qt ?

    Hi !
    Instead of waiting for our test server i am using this sample web form to play around with file posting:
    http://www.cs.tut.fi/~jkorpela/forms/file.html
    If you scroll 3 pages down there is a sample cgi that seems to fit my need 100%:
    http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi

    So if i adapt my original curl command from the first post into this:
    curl -F "textline=curltest" -F "datafile=@fddtest.fdd" http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi
    (basically just omitted user/pass and changed parameter name to fit the demo upload site)

    It works fine AND by now i have written code that also seems to satisfy that sample site ( I compared in / output with Wireshark).
    I may post it here if there are any new "ideas" apart from the links i posted above. (i am using the code from the first link).
    If i do NOT have to modify it heavily than i will not post my code version, no need to feed the entropy monster ;-)

    Thanks again for putting me on the right track...

    Nils

    Hi !
    Instead of waiting for our test server i am using this sample web form to play around with file posting:
    http://www.cs.tut.fi/~jkorpela/forms/file.html
    If you scroll 3 pages down there is a sample cgi that seems to fit my need 100%:
    http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi

    So if i adapt my original curl command from the first post into this:
    curl -F "textline=curltest" -F "datafile=@fddtest.fdd" http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi
    (basically just omitted user/pass and changed parameter name to fit the demo upload site)

    It works fine AND by now i have written code that also seems to satisfy that sample site ( I compared in / output with Wireshark).
    I may post it here if there are any new "ideas" apart from the links i posted above. (i am using the code from the first link).
    If i do NOT have to modify it heavily than i will not post my code version, no need to feed the entropy monster ;-)

    Thanks again for putting me on the right track...

    Nils


    Added after 1 28 minutes:


    Hi again !
    I got it to work completely with a demo upload server (mentioned above).
    I added some stuff to the code i found also mentioned above so i post it here if someone needs to do the same stuff.
    (Added blocking for async transfer, progress update and error handling).
    Of course the response could be too large to display it ina amessagewindow and one could add a shorter timeout when blocking for the request to end, but i do NOT need it and its simple to add,,,
    I hope this helps someone, lets see if the stuff also works on that mighty server i REALLY need to feed. If not i'll be back to ask some questions :-)

    Nils



    Qt Code:
    1. //first define the parameters: host, sub Url, <parameter> for file to be uploaded and <parameter> for filename and <
    2. QString host("www.cs.tut.fi");
    3. QString upUrl("/cgi-bin/run/~jkorpela/echo.cgi");
    4. QString upFileName("datafile");
    5. QString upFileText("textline");
    6. QString upFileDescription("QtTest");
    7. QByteArray response;
    8. QBuffer responseBuf(&response);
    9. //pre load the file to be sent, for huge files one would not load it here but give an open file aka QIODevice to the request.
    10. QString exportFName("/Users/dev/eportFile.bin");
    11. QFile exportFile(exportFName);
    12. if(!exportFile.open(QIODevice::ReadOnly))
    13. return;
    14. QByteArray fContents = exportFile.readAll();
    15. exportFile.close();
    16. //file part
    17. QHttp *http = new QHttp(this);
    18. QString boundary = "---------------------------193971182219750";
    19.  
    20. QByteArray datas(QString("--" + boundary + "\r\n").toAscii());
    21. datas += "Content-Disposition: form-data; name=\""+upFileName+"\"; filename=\""+QFileInfo(exportFName).fileName()+"\"\r\n";
    22. datas += "Content-Type: application/octet-stream\r\n\r\n";
    23.  
    24. datas += fContents;
    25. datas += "\r\n";
    26. //description part
    27. datas += QString("--" + boundary + "\r\n").toAscii();
    28. datas += "Content-Disposition: form-data; name=\""+upFileText+"\"\r\n\r\n";
    29. datas += upFileDescription+"\r\n";
    30. datas += QString("--" + boundary + "--\r\n").toAscii();
    31.  
    32. QHttpRequestHeader header("POST", upUrl);
    33. header.setValue("Host", host);
    34. header.setValue("Content-Type", "multipart/form-data; boundary=" + boundary);
    35. header.setValue("Content-Length", QString::number(datas.length()));
    36. http->setHost(host);
    37.  
    38. QEventLoop loop; // wait & block for transfer to finish for longer files may show an animation or find out how to display progress...
    39. connect(http, SIGNAL(done(bool)), &loop, SLOT(quit()));
    40. connect(http, SIGNAL(dataSendProgress(int,int)), this, SLOT(slotDataSendProgress(int, int))); //optional progress proc
    41. //actually sending & receiving stuff
    42. http->request(header, datas, &responseBuf);
    43. loop.exec();
    44. //checking for error and displaying result if successful
    45. QString resultMsg;
    46. if(http->error() != QHttp::NoError)
    47. resultMsg = tr("An error occured while trying to upload the file: \n")+http->errorString();
    48. else
    49. resultMsg = tr("Successfully transfered file, the response was:\n")+response;
    50. QMessageBox::information(this, tr("web transfer result"), resultMsg);
    51. delete http;
    52.  
    53. ...
    54. //optional progress slot:
    55.  
    56.  
    57. void MyStoopidClass::slotDataSendProgress(int done, int total)
    58. {
    59. qDebug() << done << " of " << total;
    60. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by nils_heidorn; 30th August 2013 at 14:14.

Similar Threads

  1. Simple code causes segmentation fault...in debug
    By Raccoon29 in forum Qt Programming
    Replies: 18
    Last Post: 22nd December 2009, 10:15
  2. Replies: 3
    Last Post: 11th December 2009, 18:13
  3. Replies: 2
    Last Post: 9th November 2009, 10:31
  4. running shell command from C++ code problem
    By alex chpenst in forum Qt Programming
    Replies: 4
    Last Post: 31st July 2008, 11:41
  5. Creating simple text editor from the eamples code
    By overcast in forum Qt Programming
    Replies: 4
    Last Post: 14th February 2007, 16:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.