Nominate our 2022 Qt Champions!

How do I add a custom header to an already authorized QAbstractOAuth2 object?

  • Hello,

    After being authorized, I'm using the object to make requests:
    QAbstractOAuth2::get(), QAbstractOAuth2::put(), etc.

    How to add headers to the requests made by these functions?

    Just like the function setUserAgent(), which works anytime, is there something for custom headers?


  • Lifetime Qt Champion


    Essentially a wild guess but I think prepareRequest might be what you are looking for.

  • @SGaist Thanks. =)

    That basically worked this way:

    QOAuth2AuthorizationCodeFlow *oaFlow;
    // ... *oaFlow gets authorized ...
    QNetworkRequest nrqRequest(QUrl(""));
    // I don't know what the verb/body parameters are good for:
    QNetworkReply *nrpReply=oaFlow->networkAccessManager()->get(nrqRequest);

    I don't like having to create a QNetworkRequest myself, but for a one-time use, it's fine.

    Also, at this point I already had created a custom QNetworkAccessManager re-implementing createRequest() and including some methods to add/remove raw headers. And then used QAbstractOAuth::setNetworkAccessManager() to link it to the authorized object.

Log in to reply