Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get http code request

U use QNetworkRequest to send post request. How can I get HTTP code of request? I send some request to server, on server I can see my request, but i have to check http code which server will return to application.

like image 511
Mike Avatar asked Mar 07 '16 20:03

Mike


People also ask

How can I get HTTP status code?

Just use Chrome browser. Hit F12 to get developer tools and look at the network tab. Shows you all status codes, whether page was from cache etc.

What are status code 2xx 3xx 4xx 5xx in API?

2xx successful – the request was successfully received, understood, and accepted. 3xx redirection – further action needs to be taken in order to complete the request. 4xx client error – the request contains bad syntax or cannot be fulfilled. 5xx server error – the server failed to fulfil an apparently valid request.

What is the status code for POST request?

The 204 status code is usually sent out in response to a PUT , POST , or DELETE request when the REST API declines to send back any status message or representation in the response message's body.

Whats is HTTP 201?

The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.


Video Answer


1 Answers

QNetworkRequest can not be used without QNetworkAccessManager that's responsible for making the actual request to the web server. Each request done by QNetworkAccessManager instance returns QNetworkReply where you should look for the status code from the server. It's located inside the QNetworkReply instance headers.

The request is asynchronous so it can be catch when signal is triggered.

Easiest example would be:

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

manager->get(QNetworkRequest(QUrl("http://qt-project.org")));

Then in the slot implementation:

void replyFinished(QNetworkReply *resp){
    QVariant status_code = resp->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    status_code.is_valid(){
        // Print or catch the status code
        QString status = status_code.toString(); // or status_code.toInt();
        qDebug() << status;
    }
}

Have a look on the official documentation. It explains all in details.

  • QNetworkRequest
  • QNetworkAccessManager
like image 176
Mr.Coffee Avatar answered Nov 10 '22 16:11

Mr.Coffee