Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. http request get/post
Forum Updated to NodeBB v4.3 + New Features

http request get/post

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 2.0k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    Damian7546
    wrote on last edited by
    #1

    Hi,
    I try send post request form qt appliacation . I need send request like below:

    curl --request POST \
    --url http://192.168.1.33:22222/req \
    --header 'Content-Type: application/json' \
    --header 'deviceType: arf tuf_r1' \
    --data '{
    "number":"33"
    }'
    

    The post function should look like below :

    void NetworkWorker::post(QString location)
    {
        QJsonObject obj;
        obj["number"] = "33";
        
        QNetworkRequest request =  QNetworkRequest(QUrl(location));
        request.setHeader(QNetworkRequest::ContentTypeHeader, "Content-Type: application/json");
        request.setRawHeader("deviceType", "arf tuf_r1");
        
        QNetworkReply *reply = manager.post(request, QJsonDocument(obj).toJson());
        connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::_readyRead);
    }
    
    

    does my function in qt correspond to curl notation? I'm not sure about the header .....

    JonBJ 1 Reply Last reply
    0
    • D Damian7546

      @JonB Thanks for response, What do you think when I should delete reply object ? It necessery ? If function will be called cyclicly that may be crash my app , without delete reply ?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @Damian7546

      connect(&manager, &QNetworkAccessManager::finished, [](QNetworkReply* reply)->void { reply->deleteLater(); } );
      
      1 Reply Last reply
      1
      • D Damian7546

        Hi,
        I try send post request form qt appliacation . I need send request like below:

        curl --request POST \
        --url http://192.168.1.33:22222/req \
        --header 'Content-Type: application/json' \
        --header 'deviceType: arf tuf_r1' \
        --data '{
        "number":"33"
        }'
        

        The post function should look like below :

        void NetworkWorker::post(QString location)
        {
            QJsonObject obj;
            obj["number"] = "33";
            
            QNetworkRequest request =  QNetworkRequest(QUrl(location));
            request.setHeader(QNetworkRequest::ContentTypeHeader, "Content-Type: application/json");
            request.setRawHeader("deviceType", "arf tuf_r1");
            
            QNetworkReply *reply = manager.post(request, QJsonDocument(obj).toJson());
            connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::_readyRead);
        }
        
        

        does my function in qt correspond to curl notation? I'm not sure about the header .....

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @Damian7546 I am not an expert here, but it looks right to me... :)

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Damian7546
          wrote on last edited by
          #3

          @JonB Thanks for response, What do you think when I should delete reply object ? It necessery ? If function will be called cyclicly that may be crash my app , without delete reply ?

          JonBJ 1 Reply Last reply
          0
          • D Damian7546

            @JonB Thanks for response, What do you think when I should delete reply object ? It necessery ? If function will be called cyclicly that may be crash my app , without delete reply ?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Damian7546

            connect(&manager, &QNetworkAccessManager::finished, [](QNetworkReply* reply)->void { reply->deleteLater(); } );
            
            1 Reply Last reply
            1
            • D Offline
              D Offline
              Damian7546
              wrote on last edited by Damian7546
              #5

              @JonB said in http request get/post:

              connect(&manager, &QNetworkAccessManager::finished, [](QNetworkReply* reply)->void { reply->deleteLater(); } );

              In NetworkWorker constructor it may be ?

              I get warrning:
              Pass a context object as 3rd connect parameter [clazy-connect-3arg-lambda]

              JonBJ 1 Reply Last reply
              0
              • D Damian7546

                @JonB said in http request get/post:

                connect(&manager, &QNetworkAccessManager::finished, [](QNetworkReply* reply)->void { reply->deleteLater(); } );

                In NetworkWorker constructor it may be ?

                I get warrning:
                Pass a context object as 3rd connect parameter [clazy-connect-3arg-lambda]

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #6

                @Damian7546 said in http request get/post:

                In NetworkWorker constructor it may be ?

                No, because reply does not exist there.

                Pass a context object as 3rd connect parameter [clazy-connect-3arg-lambda]

                Ignore or pass I guess this or maybe &manager as context object for slot to shut it up.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Damian7546
                  wrote on last edited by
                  #7

                  @JonB ok, please tell me if the notation below
                  connect(&manager, &QNetworkAccessManager::finished, this, [&](){reply->deleteLater();});

                  inside my function where reply object is created ,works the same like yours proposition ?

                  JonBJ 1 Reply Last reply
                  0
                  • D Damian7546

                    @JonB ok, please tell me if the notation below
                    connect(&manager, &QNetworkAccessManager::finished, this, [&](){reply->deleteLater();});

                    inside my function where reply object is created ,works the same like yours proposition ?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #8

                    @Damian7546 Yep, looks fine.

                    D 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Damian7546 Yep, looks fine.

                      D Offline
                      D Offline
                      Damian7546
                      wrote on last edited by
                      #9

                      @JonB Unfortunatelly when I use my solution my app crashed...

                      @JonB
                      This is the samereply : ? - which one i markd on red color:
                      Bez tytułu.jpg

                      JonBJ 1 Reply Last reply
                      0
                      • D Damian7546

                        @JonB Unfortunatelly when I use my solution my app crashed...

                        @JonB
                        This is the samereply : ? - which one i markd on red color:
                        Bez tytułu.jpg

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #10

                        @Damian7546
                        Look in the debugger for the reason for the crash. Make sure reply is not null. Make sure you have not deleted it more than once. You may be able to attach to its QObject::destroyed() signal to see when that happens.

                        D 1 Reply Last reply
                        2
                        • JonBJ JonB

                          @Damian7546
                          Look in the debugger for the reason for the crash. Make sure reply is not null. Make sure you have not deleted it more than once. You may be able to attach to its QObject::destroyed() signal to see when that happens.

                          D Offline
                          D Offline
                          Damian7546
                          wrote on last edited by
                          #11

                          @JonB Your solution works. ...

                          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