Important: Please read the 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 (

    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;
        QNetworkAccessManager *nwam = new QNetworkAccessManager;
           QNetworkRequest request(QUrl("http:///"));
           QByteArray data;
           QUrlQuery params;
           params.addQueryItem("user", username );
           params.addQueryItem("action", action);
           QNetworkReply *reply = nwam->post(request,data);
           QEventLoop event;
           QString html = reply->readAll();
           QByteArray bytes = reply->readAll();
           QString str = QString::fromUtf8(, 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

    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