1

i'm struggling with a code snippet for days now, i was wondering if someone could help me understand this code snippet. i'm not asking for code whatsoever, just someone to explain to me this please : (a uri appear to be the complete url to a service)

    void RestClient::_prepareRequest( QNetworkRequest& a_request, const QString& a_uri ){ 
        QSslConfiguration config(QSslConfiguration::defaultConfiguration());
        config.setProtocol(QSsl::SslV3);
        config.setSslOption(QSsl::SslOptionDisableServerNameIndication, true);
        a_request.setSslConfiguration(config);
        a_request.setRawHeader("Accept", "application/xml");
        a_request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
        QByteArray l_api_key; l_api_key.append( toQString( m_api_key) );
        QByteArray l_request_hash; 
    l_request_hash.append( toQString( _buildRequestHash( toStlString(a_uri) ) ) );
        a_request.setRawHeader("EMApikey", l_api_key );
        a_request.setRawHeader("EMRequestHash", l_request_hash );


        a_request.setUrl( QUrl( a_uri ) );
    }
2
  • 2
    a_uri seems to be the URL. The rest assembles a http(s) request. With which bits do you struggle particularly? Nov 4, 2013 at 10:15
  • Thank you for answering Mr Osterfeld. I'm struggling with the setRawHeader methods what are they for ? i couldn't find a answer. Also why the Http request is that long. i saw some requests that are way shorter than this one. Thank you in advance for any answer. Nov 4, 2013 at 10:20

1 Answer 1

2

So what you've got there is a function taking two parameters, a reference to a QNetworkRequest and a constant reference to a QString for the URI you wish to access. The next lines sets QSslConfiguration to get the default SSL configuration for Qt's network access, and stores it in config. It then sets some further QSsl options and then sets the a_request's SSL settings to be provided by the config you've just set.

Next up it sets some HTTP headers for the request, so these are reasonably standardised, so the Accept references what kind of information is acceptable for the response from the server which in this case is xml (Accept header documentation). The Content-type tells the receiving server what sort of data you're sending in the request body.

The final stage sets a non-standard HTTP header, which is for the application API access key, after that it sets the URL you originally passed and the function is complete. After that the QNetworkRequest can be used with QNetworkAccessManager to send a request to a server, with an API key encoded in, and you'll receive an XML response in return.

3
  • Thank you for taking the time to answer, great explanation ! thank you very much ! Nov 4, 2013 at 11:12
  • If it is not curious from me, can i ask you if you code in objective-c for ios ? Nov 4, 2013 at 11:27
  • Yeah, I do a bit of Objective-C as well. Nov 4, 2013 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.