Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Put request in Qt using QNetworkAccessManager

Put request in Qt using QNetworkAccessManager

Scheduled Pinned Locked Moved Mobile and Embedded
qnetworkaccessmput
6 Posts 2 Posters 6.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • nimadidN Offline
    nimadidN Offline
    nimadid
    wrote on last edited by
    #1

    Hi,

    I am writing an iPhone app in Qt Creator to do a Put request using the QNetworkAccessManager class. The request is sent to RESTful endpoint. I have python code that accomplishes what I need but for a different type of device so I'm just trying to do the same thing in C++ in Qt Creator. I will post the working python code followed by my attempt to do the same in Qt.

    Working Python code

    request = urllib2.Request(My_Url, data=body)
    request.add_header('Content-Type', 'text/plain')
    base64string = base64.encodestring('%s:%s' % (username,password)).replace('\n','')
    request.add_header("Authorization", "Basic %s" % base64string)
    request.get_method = lambda: 'PUT'
    urllib2.urlopen(request)
    

    Attempt in Qt

    QNetworkAccessManager manager;
    QByteArray PIStr = "Some data";
    QNetworkRequest request(QUrl(My_Url));
    request.setHeader(QNetworkRequest::ContentTypeHeader, QString("text/plain"));
    QString concatenated = username + ":" + password;
    QByteArray basicAuth = concatenated.toLocal8Bit().toBase64();
    QString headerData = "Basic " + basicAuth;
    request.setRawHeader("Authorization",headerData.toLocal8Bit());
    QNetworkReply* reply = manager.put(request,PIStr);
    

    When I run the above Qt code I don't see any indication from the RESTful endpoint that data is being received. Is the python code doing something different from what I wrote in Qt that I'm missing? This is my very first experience developing a mobile app so I could be missing some sort of setup step like enabling the app to access the phone's network resources for example.

    Hope I stated the problem clearly and thanks for any help possible,
    Nick

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      If your attempt in Qt code is put in a function, your manager variable probably gets out of scope and destroyed before the request has been sent.

      On a side note, there's no need for harderData to be a QString. Make it a QByteArray and avoid an unnecessary conversion. Same goes for concatenated.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      nimadidN 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        If your attempt in Qt code is put in a function, your manager variable probably gets out of scope and destroyed before the request has been sent.

        On a side note, there's no need for harderData to be a QString. Make it a QByteArray and avoid an unnecessary conversion. Same goes for concatenated.

        nimadidN Offline
        nimadidN Offline
        nimadid
        wrote on last edited by
        #3

        @SGaist
        Thank you for the quick reply. Yes the Qt code is being called in a function so the manager variable likely was going out of scope. I changed the QString variables to QByteArray as you suggested and added a QEventLoop to make sure the request is fully sent, but I'm still not seeing anything come through on the RESTful endpoint. Here is my updated code:

        QNetworkAccessManager manager;
        QByteArray PIStr = "Some data";
        QNetworkRequest request(QUrl(My_Url));
        request.setHeader(QNetworkRequest::ContentTypeHeader, QString("text/plain"));
        QByteArray concatenated = username + ":" + password;
        QByteArray basicAuth = concatenated.toBase64();
        QByteArray headerData = "Basic " + basicAuth;
        request.setRawHeader("Authorization",headerData);
        QNetworkReply* reply = manager.put(request,PIStr);
        
        QEventLoop loop;
        connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
        loop.exec();
        

        Will this event loop correctly wait until the put request has completed?

        Thanks again for the help.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Silly question but: did you check that your My_Url content is valid ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          nimadidN 1 Reply Last reply
          0
          • SGaistS SGaist

            Silly question but: did you check that your My_Url content is valid ?

            nimadidN Offline
            nimadidN Offline
            nimadid
            wrote on last edited by
            #5

            It definitely wasn't a silly question because it got me thinking about the fact that the IP address of my desktop is part of the URL, which led me to try a ping from my phone to the desktop. The ping was unsuccessful and I figured out it was due to the firewall running on my desktop. After disabling it, the ping now works but my Qt app still isn't successfully sending data to the REST endpoint.

            I've confirmed the actual content of My_Url is valid because my python system does a put request to the same URL and doesn't have this problem.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You should also connect the error signal to see if you it tells you something useful.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved