Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

400 Response when sending POST



  • 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.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

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



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



  • @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 ?.


    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

  • Moderators

    @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.



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


Log in to reply