Solved 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.
-
Hi and welcome to devnet,
You are mentioning
output.php
in your explanation but in your code you are sending toindex.php
. -
@SGaist
Thanks for that fast response! It was a mistake i made - the file is called index.php! Sorry for that. -
- Why are you using
data.remove(0,1);
to remove the first character of data bytearray ? - You have mentioned 2 parameters: name and action but in post request, you have used user and action, are these the correct parametes ?.
- Why are you using
-
- I honestly dont know. Tried something from Google and forgot to remove it.
- 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
-
@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.