Search code examples
iosgoogle-app-enginegoogle-cloud-endpoints

Add custom headers to Cloud Endpoints call in iOS (Swift) using iOS client library


How do you add custom headers when executing API calls with Cloud Endpoints in an iOS app (with Swift)?

Here is the same question answered for Android, but I cannot find an iOS example anywhere:

Modify HTTP headers in Google App Engine Endpoints (Android)

Thanks!


Solution

  • You can use the additionalHTTPHeaders property associated with each GTLService base class. For e.g.

    var service = GTLServiceTictactoe()
    service.retryEnabled = YES
    service.additionalHTTPHeaders = ["my custom header name " : "my custom header value"]
    ....
    

    Here is the description of this property from documentation

    /**
     *  Any additional HTTP headers for this queries executed by this service.
     *
     *  Individual queries may have additionalHTTPHeaders specified as well.
     */
    @property(atomic, copy, nullable) NSDictionary<NSString *, NSString *> *additionalHTTPHeaders;