Skip to main content

Request Data Related Interfaces

Custom Request Header

The SDK supports configuring the collection of 'request header' parameters for 'backend call chain tracing'.

  • Related Interface
/**
* @key Set to the header key value of the request header to be collected
*/
+(void)setRequestIDHeaderKey:(NSString *)key;
  • Code Example
int main(int argc, char * argv[]) {
@autoreleasepool {
// It is recommended to call this at initialization, and it will not be collected for the first time in other locations
[NBSAppAgent startWithAppID:@"appkey"];
[NBSAppAgent setRequestIDHeaderKey:@"key"];
...
}
}

SDK Data Transmission Using GM Encryption

The SDK supports GM encryption for data transmission. Call this interface to enable GM encryption (default is not enabled, prerequisite: NBSGMKit.framework must be included in the project).

Note: This interface needs to be called before SDK initialization.

  • Related Interface
/**
* @need Pass YES to enable GM encryption for SDK data transmission.
*/
+ (void)encryptionRequired:(BOOL)need;
  • Code Example
int main(int argc, char * argv[]) {
@autoreleasepool {
//Enable GM encryption
[NBSAppAgent encryptionRequired:YES];
//Default is https, if redirect_host is http protocol then this interface needs to be called.
[NBSAppAgent setHttpEnabled:YES];
//redirect_host is the platform server address
[NBSAppAgent setRedirectURL:@"redirectUrl"];
//Your_appkey is obtained from the platform
[NBSAppAgent startWithAppID:@"APPkey"];
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}

IM Security SDK Request Collection Switch

IM Security SDK request collection switch, default is off, call this interface to enable.

  • Related Interface
/**
* @brief Enable IM Security SDK request monitoring, default is off
* @note
* The principle of Tingyun SDK monitoring IM Security SDK requests is to intercept the interface through which IM Security SDK sends requests to obtain performance data. If the request interface of IM Security SDK is updated (method name change or parameter change), it may lead to interception failure, and it may even cause a crash. Therefore, after enabling this feature or updating IM Security SDK, testing should be performed.
*/
+ (void)startIMSecurityMonitor;

  • Code Example
int main(int argc, char * argv[]) {
NSString * appDelegateClassName;
@autoreleasepool {
...
[NBSAppAgent startIMSecurityMonitor];//Must be called before SDK start
[NBSAppAgent setRedirectURL:@"redirectUrl"];
[NBSAppAgent startWithAppID:@"APPkey"];
...
}
}

Support for Collecting libcurl Request Data

Obtain libcurl request performance data, such as IP, DNS, TCP, SSL, and other performance metrics.

  • Related Interface
/**
* for record curl networl.
*/
void nbsGetCurlNetworkInfo(void *curl,int curlcode,void *ptr);
  • Code Example
- (void)libcurlNetwork
{
...
CURL *curl = curl_easy_init();
CURLcode res;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.baidu.com");
res = curl_easy_perform(curl);
nbsGetCurlNetworkInfo(curl, res, &curl_easy_getinfo);
curl_easy_cleanup(curl);
}
...
}

Request Content Collection

After calling this interface, the SDK will collect the request header, response header, request body, and response body of Http/Https requests.

When the Content-Type value in the request header is application/json, application/x-www-form-urlencoded, or text/plain, the request body will be collected.

When the Content-Type value in the response header is application/json or text/plain, the response body will be collected.

Note: When the platform's 'Collect Network Request Content' is enabled and this interface is called, the request content collection feature is enabled.

  • Related Interface
/**
* Enable the acquisition of request content
*/
+ (void)enableNetworkContentRecord;
  • Code Example
int main(int argc, char * argv[]) {
NSString * appDelegateClassName;
@autoreleasepool {
...
[NBSAppAgent enableNetworkContentRecord];
[NBSAppAgent setRedirectURL:@"redirectUrl"];
[NBSAppAgent startWithAppID:@"appkey"];

...
appDelegateClassName = NSStringFromClass([AppDelegate class]);
}
return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}

Custom Request Header/Body Collection

After calling this interface, you can customize the collection of request headers/request bodies. If called multiple times, it will override the previous settings.

Note: If both the platform and the interface are configured for request header collection, the interface configuration takes precedence.

  • Related Interface
/**
* Set up a custom request header and request body acquisition method
*/
+ (void)setupRequestSanitizer:(void (^)(NBSRequestSanitizer *requestSanitizer))configureSanitizer;
  • Code Example

    • Request Header Collection
     [NBSAppAgent setupRequestSanitizer:^(NBSRequestSanitizer *requestSanitizer) {
    [requestSanitizer setRequestHeaderSanitizionWithBlock:^NSDictionary * _Nullable(NSURL * _Nonnull url, NSDictionary * _Nonnull allHTTPHeaderFields) {
    // When the requested URL includes www.tingyun.com, do not collect the traceparent and traceid request headers
    if([url.absoluteString containsString:@"www.tingyun.com"])
    {
    NSMutableDictionary *dict = [allHTTPHeaderFields mutableCopy];
    [dict removeObjectsForKeys:@[@"traceparent",@"traceid"]];
    // Return the custom collected request headers
    return dict;
    }
    // Return all request headers
    return allHTTPHeaderFields;
    }];
    }];
    • RequestBody Collection
    [NBSAppAgent setupRequestSanitizer:^(NBSRequestSanitizer *requestSanitizer) {
    [requestSanitizer setRequestBodySanitizionWithBlock:^NSString * _Nullable(NSURL * _Nonnull url, NSDictionary * _Nonnull allHTTPHeaderFields, NSData * _Nonnull body) {
    // When the requested URL includes www.tingyun.com, return custom request body information
    if([url.absoluteString containsString:@"www.tingyun.com"])
    {
    // Return custom request body information
    return @"";
    }
    NSString *reqBody = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
    return reqBody;
    }];

    }];

You can configure the request header and request body together.

Custom Response Header/Body Collection

After calling this interface, you can customize the collection of response headers/response bodies. If called multiple times, it will override the previous settings.

Note: If both the platform and the interface are configured for response header collection, the interface configuration takes precedence.

  • Interface
/**
* Set up a custom response header and response body acquisition method
*/
+ (void)setupReponseSanitizer:(void (^)(NBSResponseSanitizer *responseSanitizer))configureSanitizer;
  • Example

    • Response Header Collection
    [NBSAppAgent setupReponseSanitizer:^(NBSResponseSanitizer *responseSanitizer) {
    [responseSanitizer setResponseHeaderSanitizionWithBlock:^NSDictionary * _Nullable(NSHTTPURLResponse * _Nonnull response) {
    // When the requested URL includes www.tingyun.com, do not collect the x-Request-Id and Tracecode response headers
    if( [response.URL.absoluteString containsString:@"www.tingyun.com"]){
    NSMutableDictionary *dict = [response.allHeaderFields mutableCopy];
    [dict removeObjectsForKeys:@[@"x-Request-Id",@"Tracecode"]];
    // Return the custom collected response headers
    return dict;
    }
    // Return all response headers
    return response.allHeaderFields;
    }];
    }];
    • ResponseBody Collection
        [NBSAppAgent setupReponseSanitizer:^(NBSResponseSanitizer *responseSanitizer) {

    [responseSanitizer setResponseBodySanitizionWithBlock:^NSString * _Nullable(NSHTTPURLResponse * _Nonnull response, NSData * _Nonnull body) {
    // When the requested URL includes www.tingyun.com, return custom response body information
    if([response.URL.absoluteString containsString:@"www.tingyun.com"])
    {
    // Return custom response body information
    return @"";
    }
    NSString *reqBody = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
    return reqBody;
    }];

    }];

You can configure the response headers and response body together.