Version 2021

Add Custom Headers to Network Requests in iOS

You can customize the information sent in a network request in a number of ways. For example, you can set custom URL parameter key/value pairs, or you can add customized HTTP  headers to the request. Customized request headers are useful for passing additional information in the request. One example use case would be users who want to add special authentication information, such as Good Dynamics tokens, to the headers of HTTP requests.

The following APIs can be used when you want to pass information in customized request headers:

  • -(void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;

    This method sets the value of the given customized request header field. If a value was previously set for the given header field, that value is replaced with the given value.

    Parameters:
    value: Header field value
    field: Header field name (case-insensitive)

  • -(NSString*)valueForHTTPHeaderField:(NSString*)field;

    This method returns the value that corresponds to the given customized request header field.

    Parameter:
    field: Header field name to use for the lookup (case-insensitive)

    Result:
    Value associated with the given customized request header field, or nil if there is no value associated with the given header field.

  • -(NSArray *)getHTTPHeaderFields;
    This method returns an array of all the fields in the customized request header dictionary. The dictionary for the customized header key/value pairs is stored in SDKEnvSettings.
  • -(void)removeHTTPHeaderField:(NSString*)field;
    This method removes a customized header value for a given customized request header field.

    Parameter:
    field: Header field name to use for the lookup (case-insensitive)

  • -(void)clearHTTPHeaderFields;
    This method removes all customized request headers.

The dictionary for the customized header key/value pairs is stored in SDKEnvSettings.