Results 1 to 13 of 13

Thread: POST Request problems with IDrive EVS REST APIs

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default POST Request problems with IDrive EVS REST APIs

    I'm trying to use IDrive EVS REST APIs to upload a file with this service witout success.

    This is the upload api (http://evs.idrive.com/web-file-upload-api.htm)
    Upload local file(s) to your IDrive account
    API Call: https://<server address>/evs/uploadFile
    Method POST
    Parameters uid=USERNAME&pwd=PASSWORD&pvtkey=OPTIONAL_PRIVATE_ KEY&p=DESTPATH&myfiles=FILENAME
    enctype multipart/form-data

    I try this piece of code but doesn't work. IDrive says "REQUEST 'ENCTYPE' IS NOT MULTIPART FORM DATA"

    Qt Code:
    1. QFile *file = new QFile(sourcePath);
    2. file->open(QIODevice::ReadOnly);
    3.  
    4. QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    5. QHttpPart userNamePart;
    6. userNamePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"uid\""));
    7. userNamePart.setBody(this->_userName.toUtf8());
    8. QHttpPart passwordPart;
    9. passwordPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"pwd\""));
    10. passwordPart.setBody(this->_password.toUtf8());
    11. QHttpPart destPart;
    12. destPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"p\""));
    13. destPart.setBody(destPath.toUtf8());
    14.  
    15. QHttpPart imagePart;
    16. imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
    17. imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\"; filename=\"" + file->fileName().toUtf8()+"\"\r\n\r\n\r\n"));
    18. imagePart.setBodyDevice(file);
    19. file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
    20.  
    21. multiPart->append(userNamePart);
    22. multiPart->append(passwordPart);
    23. multiPart->append(destPart);
    24. multiPart->append(imagePart);
    25.  
    26. QNetworkRequest req;
    27. req.setUrl(QUrl(QString("https://%0/evs/uploadFile").arg(this->_serverAddress)));
    28.  
    29. if (this->syncPost(req, multiPart)) {
    30. return (this->_reply.result()["message"] == "SUCCESS");
    31. }
    32. return false;
    33.  
    34. // req.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; charset=UTF-8; boundary=" + boundary);
    35. // QFile file(sourcePath);
    36. // file.open(QIODevice::ReadOnly);
    37. // QByteArray postData(QString("uid=%0&pwd=%1&p=%3&myfiles=%4")
    38. // .arg(this->_userName)
    39. // .arg(this->_password)
    40. // .arg(destPath)
    41. // .arg(file.fileName()).toUtf8());
    42. // postData.append("--" + boundary + "\r\n");
    43. // postData.append("Content-Type: image/jpeg\r\n\r\n");
    44. // postData.append(file.readAll());
    45. // file.close(); // the file is opened earlier in the code
    46. // postData.append("\r\n");
    47. // postData.append("--" + boundary + "\r\n");
    48. // int contentLength = postData.length();
    49. // req.setHeader(QNetworkRequest::ContentLengthHeader, contentLength);
    50.  
    51. }
    To copy to clipboard, switch view to plain text mode 

    I also try with the commented code but I got the same error!

    How should I do?

    Albano

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

    Default Re: POST Request problems with IDrive EVS REST APIs

    Check if the data is properly formatted before sending it.
    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.


  3. #3
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: POST Request problems with IDrive EVS REST APIs

    How can I see the raw data that I send?
    I can't capture tcp packets with Wiresharsk becouse I have to use a https connection.

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

    Default Re: POST Request problems with IDrive EVS REST APIs

    You don't have to send that data to this particular server, you know... you just need to see the outgoing request.
    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.


  5. #5
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: POST Request problems with IDrive EVS REST APIs

    I've sent the request to a my php page that should print all the post request but I can't understand the problem.

    I can upload file with this piece of code
    Qt Code:
    1. QFile fileUp("FILE PATH");
    2. fileUp.open(QIODevice::ReadOnly);
    3. QByteArray file(fileUp.readAll());
    4. fileUp.close();
    5.  
    6. QByteArray boundary("AaB03x");
    7.  
    8. QNetworkRequest req(QUrl("https://evsweb39.idrive.com/evs/uploadFile"));
    9. req.setRawHeader("Content-Type", QByteArray("multipart/form-data; charset=UTF-8; boundary=") + boundary);
    10.  
    11. QByteArray postData;
    12. postData += "--" + boundary + "\r\n";
    13. postData += "Content-Disposition: form-data; name=\"uid\"\r\n\r\n";
    14. postData += userName + "\r\n";
    15.  
    16. postData += "--" + boundary + "\r\n";
    17. postData += "Content-Disposition: form-data; name=\"pwd\"\r\n\r\n";
    18. postData += passWord + "\r\n";
    19.  
    20. postData += "--" + boundary + "\r\n";
    21. postData += "Content-Disposition: form-data; name=\"p\"\r\n\r\n";
    22. postData += remotePath + "\r\n";
    23.  
    24. postData += "--" + boundary + "\r\n";
    25. postData += "Content-Disposition: form-data; filename=\"" + fileName + "\";\r\nContent-Type: application/octet-stream\r\n\r\n";
    26. postData += file;
    27. postData += "\r\n";
    28.  
    29. postData += "--" + boundary + "--\r\n";
    30.  
    31. req.setHeader(QNetworkRequest::ContentLengthHeader, QString::number(postData.length()));
    To copy to clipboard, switch view to plain text mode 

    How can I do it using QHttpMultiPart?

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

    Default Re: POST Request problems with IDrive EVS REST APIs

    Did you test the output you get using QHttpMultiPart?
    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.


  7. #7
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: POST Request problems with IDrive EVS REST APIs

    What I see with my custom php page is the same but I'm not sure. I've got some problems retrieving the raw post request by php.

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

    Default Re: POST Request problems with IDrive EVS REST APIs

    Use netcat instead of the php servlet.
    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.


  9. #9
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: POST Request problems with IDrive EVS REST APIs

    Great! With netcat I can see the difference!

    QHttpMultiPart add this header: MIME-Version: 1.0
    Is this important? Why if I don't add this header I can upload this file. In the examples of RFC1867 they don't add the MIME version (http://www.faqs.org/rfcs/rfc1867.html). Why Qt does it?

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

    Default Re: POST Request problems with IDrive EVS REST APIs

    Quote Originally Posted by PaceyIV View Post
    QHttpMultiPart add this header: MIME-Version: 1.0
    Is this important?
    Apparently so.

    Why Qt does it?
    It's a standard header so I don't see why it would cause your message to fail. I would rather assume there is some other difference you are missing.
    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. #11
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: POST Request problems with IDrive EVS REST APIs

    You're right!
    I add the MIME version header with my working code and it continues to work, so I found the real difference! QHttpMultipart doesn't add "\r\n" to the end of the request. If I don't do this the server rejects the request!

    What I can do?

Similar Threads

  1. ASP .NET - POST request method
    By Trok in forum Qt Programming
    Replies: 3
    Last Post: 6th October 2011, 22:52
  2. Request ID of QNetworkaccessmanager get and post request
    By dineshkumar in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2011, 21:56
  3. Replies: 1
    Last Post: 8th September 2009, 21:56
  4. POST request to a web service
    By QPlace in forum Qt Programming
    Replies: 3
    Last Post: 6th November 2008, 08:05
  5. Https POST Request
    By munna in forum Qt Programming
    Replies: 10
    Last Post: 11th November 2006, 14:24

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.