QNetworkaccessmanager - post



  • Hello
    When i try to figure out how to use this QNAM and post I Tried this.

    I will fill the bytearray with some id (Numeric) and make it as a list.

    It works with one value but not with 2 or more and I recive an Unkown error in all of my cases.

    Where is the missing part.

    The contact to the url-- is all right

    I fill up the bytearray in a loop
    @ QByteArray postData;

    for (int i = 0; i < selectedToAck.size(); ++i)
    {
    

    postData.append("m_iFuelSensor=");
    postData.append(model->item(index.row(),0)->data(Qt::DisplayRole).toString());@

    @/*

    • connection();

    • param postdata- bytearray

    • try to do a form post with QNetworkAccessmanager
      */
      void Logs::connection( QByteArray postData)
      {

      manager = new QNetworkAccessManager(this);
      connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(finishedSlot(QNetworkReply*)));

    QUrl url(globalP-&gt;getGCUUrl() + "/fuelParams.htm? ");
    reply = manager->post(QNetworkRequest(url),postData);
    

    }

    /*

    • finishedSlot();

    • param replay- QNetworkReply

    • answer of the post metchod
      /
      void Logs::finishedSlot(QNetworkReply
      reply)
      {

      QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute );

      QMessageBox msgBox;
      QVariant tempQ = QVariant(statusCodeV.toString());
      msgBox.setText(tempQ.toString() + " " + reply->errorString());
      int ret = msgBox.exec();

      // "200 OK" received?
      if (statusCodeV.toInt()==200)
      {

       msgBox.setText("Successfull!...");
       int ret = msgBox.exec&#40;&#41;;
      
       QByteArray bytes = reply->readAll(); // bytes
       const QString string(bytes); // string
      
       QMessageBox msgBox;
       msgBox.setText(string );
       msgBox.exec&#40;&#41;;
      

      }

      else
      {
      msgBox.setText("UnSuccessfull!...");
      int ret = msgBox.exec();
      }

      selectedToAck.clear();
      setActiveAlarms();

      delete reply;
      delete manager;

    }
    @



  • Was your loop truncated or you're missing the "&" to join the arguments?

    Also: don't do that by hand. Use QUrl to build the query string, or QHttpMultipart.



  • I didn't get that either to work properly.
    Do you have an example of that



  • I even got this in the debugwindow

    bq. X Error: BadWindow (invalid Window parameter) 3
    Major opcode: 15 (X_QueryTree)
    Resource id: 0x165b0ef
    X Error: BadWindow (invalid Window parameter) 3
    Major opcode: 40 (X_TranslateCoords)
    Resource id: 0x165b0ef
    X Error: BadWindow (invalid Window parameter) 3
    Major opcode: 15 (X_QueryTree)
    Resource id: 0x165c579
    X Error: BadWindow (invalid Window parameter) 3
    Major opcode: 40 (X_TranslateCoords)
    Resource id: 0x165c579
    X Error: BadWindow (invalid Window parameter) 3
    Major opcode: 15 (X_QueryTree)
    Resource id: 0x165e494
    X Error: BadWindow (invalid Window parameter) 3
    Major opcode: 40 (X_TranslateCoords)
    Resource id: 0x165e494



  • In your (truncated) for cycle you are assigning the same parameter multiple times. Is that what you really want to achieve? I believe you want to concat the values into a single variable.



  • i want to post
    postData.append("m_iFuelSensor=") ;// Value
    as a list to the server.

    it could be one or many values
    not as a singel variabel;



  • @ QByteArray postData;
    postData.append("m_iFuelSensor=");
    for (int i = 0; i < selectedToAck.size(); ++i)
    postData.append(model->item(index.row(),0)->data(Qt::DisplayRole).toString());
    @



  • No "?" ?
    Because now all the values are in the variabel and its gonna be a big number.
    Shouldn't i have some sort of delimiter maybe



  • More errors
    I got the errorcode 302 unknown error when i do my post but it changes the value in the textbox.
    if I send one value.
    When i changed to get operation I got the errorcode 200 "OK"
    If someone have a simple example to show or some great hints to get me go on with this "Problem"
    I would be very happy.



  • [quote author="toho71" date="1324472734"]No "?" ?
    Because now all the values are in the variabel and its gonna be a big number.
    Shouldn't i have some sort of delimiter maybe[/quote]

    You are right, at each iteration you have to add a delimiter.



  • Ok thank you but it still doesn't work as i expected.
    Same problems and no update of the webpage.
    Maybe I have to do it in another way becaus when I use ex. Wireshark I dont see the incoming values in my postoperation.
    Strange.



  • What if you try to make a GET request? Just to see if the data is sent or not.



  • I got the errorcode 302 unknown error when i do my post but it changes the value in the textbox when i update the webpage and if I send one value?

    When i changed to get operation I got the errorcode 200 == “OK”



  • I would start doing a post request with another tool, let say wget, and see how the request must be done and then reproduce it in Qt.



  • This can't be a problem in qt but I don't know how to do it properly.

    It must be someone else who have done this before.

    Maybe I have to use Qhttp instead



  • [quote author="toho71" date="1324560334"
    Maybe I have to use Qhttp instead
    [/quote]

    You will run into the very same problems with constructing the URL and/or preparing the post data. You will need to understand HTTP protocol basics before using it in a class.



  • I'm sorry but after reading more about this subject I'm still not able to go further so If some nice person would tell me a little more how to take the next step so I got this code working with Post request it would be great.



  • Do you have the exact list of parameters as they have to be sent to the server to perform a correct post request? How is your code working now and what is the exact problem?



  • I have a list in a table and when I check (Chkbox) some rows in that table I got a list of numbers that I want to post to a list on the server. It works with one value when I refresh the browserwindow but not with more values.

    //T



  • I suspect your cgi/web application does not allow you to set multiple values at once, or has a special formatting for such values. Do you have the specifics of the parameters your web application is accepting? Otherwise you have to guess and try.



  • Ok thank you for the tip.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.