Qnetworkrequest Plus authorization head, adapted to rest-style APIs

Source: Internet
Author: User

Rest is stateless. There should be no dependency between rest requests, and it is not necessary to call another request before calling a request. There should not be a session in rest, especially the rest request should not save the information in sesssion for use in subsequent calls. Even if security authentication is included, the client should not need to log in early and then save the permission information in the session, and the subsequent request is invoked with the same session.

The way to implement stateless is to include all information in the current request, including validation information. HTTP is stateless, HTTP has a authorization header, HTTP requirements are in each request to put the authentication information inside, the server every time before processing the request to verify the information. For security, we can provide a Rest API that generates tokens, which the client calls to generate tokens (a username/password can be attached to generate tokens). This token is placed in the authentication header in all subsequent requests.

The following code is the implementation of Qnetworkrequest plus authorization head

QString _url;//url

QString _token;//token

//

Qnetworkrequest Mreq;
Qnetworkaccessmanager *tnam = new Qnetworkaccessmanager ();

Qeventloop Loop;
Qobject::connect (Tnam, SIGNAL (Finished (qnetworkreply *)), &loop, SLOT (Quit ()));
Mreq.seturl (Qurl (_url));
Mreq.setheader (Qnetworkrequest::contenttypeheader, "application/x-www-form-urlencoded");
//
QString Token_headerdata = "token" + _token;
Mreq.setrawheader ("Authorization", Token_headerdata.tolocal8bit ());
//
qnetworkreply* Reply=tnam->get (mreq);
Loop.exec ();
Qbytearray Data=reply->readall ();

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.