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. Qt post request
Qt 6.11 is out! See what's new in the release blog

Qt post request

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

    I'm new to qt .I want to send 2 strings with post request but I couldn't find too many examples, do you have a sample or code?

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

      Then write the methods that will marshal and unmarshal your classes to/from text. You can write QDataStream operators.

      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
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Do you mean QNetworkAccessManager::post ?

        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
        1
        • R Offline
          R Offline
          Raii
          wrote on last edited by
          #3

          yes we are doing a joint project with my friend, he wants 2 strings from me, I have to post them
          I need to put 2 string in (class)model and send them

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

            Please describe your exact setup.

            Because you could send these two strings as JSON data in one request, or split that in two.

            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
            • R Offline
              R Offline
              Raii
              wrote on last edited by
              #5

              I got it but I have a question for you
              Why should I use QByteArray for post in Qt? Can't I use a direct class object instead?
              QNetworkReply * post(const QNetworkRequest &request, QIODevice *data)
              QNetworkReply * post(const QNetworkRequest &request, const QByteArray &data)
              QNetworkReply * post(const QNetworkRequest &request, QHttpMultiPart *multiPart)

              JonBJ jsulmJ 2 Replies Last reply
              0
              • R Raii

                I got it but I have a question for you
                Why should I use QByteArray for post in Qt? Can't I use a direct class object instead?
                QNetworkReply * post(const QNetworkRequest &request, QIODevice *data)
                QNetworkReply * post(const QNetworkRequest &request, const QByteArray &data)
                QNetworkReply * post(const QNetworkRequest &request, QHttpMultiPart *multiPart)

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

                @Raii
                You want to post a class object across the network? It doesn't make sense. You can post e.g. a QByteArray, you could think about serializing the object to bytes (e.g. see QDataStream) and deserialize at the other end, but not sure what you're trying to achieve.

                1 Reply Last reply
                3
                • R Raii

                  I got it but I have a question for you
                  Why should I use QByteArray for post in Qt? Can't I use a direct class object instead?
                  QNetworkReply * post(const QNetworkRequest &request, QIODevice *data)
                  QNetworkReply * post(const QNetworkRequest &request, const QByteArray &data)
                  QNetworkReply * post(const QNetworkRequest &request, QHttpMultiPart *multiPart)

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @Raii said in Qt post request:

                  Why should I use QByteArray for post in Qt?

                  Because network layer sends bytes over network, it does not know anything about your classes (but you can serialise/deserialise your classes as @JonB mentioned).
                  But nothing stops you from converting your string into byte array:
                  https://doc.qt.io/qt-5/qstring.html#toLatin1
                  https://doc.qt.io/qt-5/qstring.html#toLocal8Bit
                  https://doc.qt.io/qt-5/qstring.html#toUtf8
                  Which method to use depends on the encoding of your string and what encoding you want to use to send the data.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  5
                  • R Offline
                    R Offline
                    Raii
                    wrote on last edited by Raii
                    #8

                    @JonB

                    I can send it in c # windows form code example

                    
                    class sendEmailAll
                    {
                    
                    
                    public String title { get; set; }
                    public String message { get; set; }
                    
                        public void sendMessage(sendEmailAll message) //send to class object 
                        {
                            using (var client = new HttpClient())
                            {
                                client.BaseAddress = new Uri("http://192.168.1.33:8081");
                                client.DefaultRequestHeaders.Accept.Clear();
                                HttpResponseMessage response = client.PostAsJsonAsync("/email/sendAll", message).Result; 
                                
                              // message is class object
                    
                            }
                        }
                    }
                    
                    
                    
                    JonBJ 1 Reply Last reply
                    0
                    • R Raii

                      @JonB

                      I can send it in c # windows form code example

                      
                      class sendEmailAll
                      {
                      
                      
                      public String title { get; set; }
                      public String message { get; set; }
                      
                          public void sendMessage(sendEmailAll message) //send to class object 
                          {
                              using (var client = new HttpClient())
                              {
                                  client.BaseAddress = new Uri("http://192.168.1.33:8081");
                                  client.DefaultRequestHeaders.Accept.Clear();
                                  HttpResponseMessage response = client.PostAsJsonAsync("/email/sendAll", message).Result; 
                                  
                                // message is class object
                      
                              }
                          }
                      }
                      
                      
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #9

                      @Raii
                      And your point is what? You are using PostAsJsonAsync. I think someone earlier said you can convert to JSON, which is what this is doing. @SGaist wrote

                      Because you could send these two strings as JSON data in one request, or split that in two.

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        Raii
                        wrote on last edited by Raii
                        #10

                        @JonB @SGaist
                        i need these two
                        i will pull the get as json and throw it into the model
                        I will also send a post, sometimes a parameter and sometimes a direct model

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

                          Then write the methods that will marshal and unmarshal your classes to/from text. You can write QDataStream operators.

                          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
                          3

                          • Login

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