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. 400 Response when sending POST
Forum Updated to NodeBB v4.3 + New Features

400 Response when sending POST

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.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.
  • M Offline
    M Offline
    morlecapka
    wrote on last edited by morlecapka
    #1

    Hey,
    So first of all: im not that experienced with C++ neither Qt. This is a school project.

    Im trying to send a POST-request to my PHP-script which outputs data corresponding to the given input, so for example:
    I have two Textboxes, name and action, which should get sent to the webserver (127.0.0.1/index.php?name=morle&action=nothing)

    Now i want to store the output given by the PHP-script as a string, which should not be hard. Im pretty much done i think, but im getting a BAD REQUEST whenever i push the Button to send the Data.

    QByteArray abfrage;
        abfrage.append("username="+(ui->lineEdit_username->text())+"&action="+(ui->lineEdit_action->text()));
        QNetworkAccessManager *nwam = new QNetworkAccessManager;
           QNetworkRequest request(QUrl("http:///127.0.0.1/index.php"));
           QByteArray data;
           QUrlQuery params;
           params.addQueryItem("user", username );
           params.addQueryItem("action", action);
           data.append(params.toString());
           data.remove(0,1);
           QNetworkReply *reply = nwam->post(request,data);
           QEventLoop event;
           connect(reply,SIGNAL(finished()),&event,SLOT(quit()));
           event.exec();
           QString html = reply->readAll();
           QByteArray bytes = reply->readAll();
           QString str = QString::fromUtf8(bytes.data(), bytes.size());
           int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
           qDebug() << QVariant(statusCode).toString();
    
    

    Now the "html" string obviously gives me the BAD REQUEST code.

    I can't figure out what im doing wrong, so i hope somebody can help me out.

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

      Hi and welcome to devnet,

      You are mentioning output.php in your explanation but in your code you are sending to index.php.

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

      M 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        You are mentioning output.php in your explanation but in your code you are sending to index.php.

        M Offline
        M Offline
        morlecapka
        wrote on last edited by
        #3

        @SGaist
        Thanks for that fast response! It was a mistake i made - the file is called index.php! Sorry for that.

        KillerSmathK 1 Reply Last reply
        0
        • M morlecapka

          @SGaist
          Thanks for that fast response! It was a mistake i made - the file is called index.php! Sorry for that.

          KillerSmathK Offline
          KillerSmathK Offline
          KillerSmath
          wrote on last edited by
          #4

          @morlecapka

          1. Why are you using data.remove(0,1); to remove the first character of data bytearray ?
          2. You have mentioned 2 parameters: name and action but in post request, you have used user and action, are these the correct parametes ?.

          @Computer Science Student - Brazil
          Web Developer and Researcher
          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

          1 Reply Last reply
          1
          • M Offline
            M Offline
            morlecapka
            wrote on last edited by
            #5
            1. I honestly dont know. Tried something from Google and forgot to remove it.
            2. Yes, im passing the correct parameters. The first part was written out of my head - didnt remember the parameters i used in the code. It was just an example
            1 Reply Last reply
            0
            • M morlecapka

              Hey,
              So first of all: im not that experienced with C++ neither Qt. This is a school project.

              Im trying to send a POST-request to my PHP-script which outputs data corresponding to the given input, so for example:
              I have two Textboxes, name and action, which should get sent to the webserver (127.0.0.1/index.php?name=morle&action=nothing)

              Now i want to store the output given by the PHP-script as a string, which should not be hard. Im pretty much done i think, but im getting a BAD REQUEST whenever i push the Button to send the Data.

              QByteArray abfrage;
                  abfrage.append("username="+(ui->lineEdit_username->text())+"&action="+(ui->lineEdit_action->text()));
                  QNetworkAccessManager *nwam = new QNetworkAccessManager;
                     QNetworkRequest request(QUrl("http:///127.0.0.1/index.php"));
                     QByteArray data;
                     QUrlQuery params;
                     params.addQueryItem("user", username );
                     params.addQueryItem("action", action);
                     data.append(params.toString());
                     data.remove(0,1);
                     QNetworkReply *reply = nwam->post(request,data);
                     QEventLoop event;
                     connect(reply,SIGNAL(finished()),&event,SLOT(quit()));
                     event.exec();
                     QString html = reply->readAll();
                     QByteArray bytes = reply->readAll();
                     QString str = QString::fromUtf8(bytes.data(), bytes.size());
                     int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
                     qDebug() << QVariant(statusCode).toString();
              
              

              Now the "html" string obviously gives me the BAD REQUEST code.

              I can't figure out what im doing wrong, so i hope somebody can help me out.

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @morlecapka
              what type of server is this? Basically the server can send an 400 Error anytime if it doesn't comply it's expectations.
              Also you have 3 slashes in your request url.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              M 1 Reply Last reply
              4
              • raven-worxR raven-worx

                @morlecapka
                what type of server is this? Basically the server can send an 400 Error anytime if it doesn't comply it's expectations.
                Also you have 3 slashes in your request url.

                M Offline
                M Offline
                morlecapka
                wrote on last edited by
                #7

                @raven-worx I feel so stupid right now. The third slash was the problem. Thank you very much.

                1 Reply Last reply
                1

                • Login

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