Skip to main content

Request Data

Custom Request Headers

SDK supports configuring collection of "Request Header" parameters for "Backend Call Chain Tracking".

  • Related API
/**
* @key Set to the headerkey value in the request header to be collected
*/
+(void)setRequestIDHeaderKey:(NSString *)key;
  • Code Example
int main(int argc, char * argv[]) {
@autoreleasepool {
// Recommended calling location is during initialization, other locations won't collect on first call
[NBSAppAgent startWithAppID:@"appkey"];
[NBSAppAgent setRequestIDHeaderKey:@"key"];
...
}
}

SDK Uses National Encryption for Data Transmission

SDK data transmission supports national encryption. Call this API to enable national encryption. Default is not enabled "Prerequisite: NBSGMKit.framework needs to be imported in the project".

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

  • Related API
/**
* @need Pass YES, then SDK uses national encryption for data transmission.
*/
+ (void)encryptionRequired:(BOOL)need;
  • Code Example
int main(int argc, char * argv[]) {
@autoreleasepool {
//Enable national encryption
[NBSAppAgent encryptionRequired:YES];
//Default is https, if redirect_host is http protocol, you need to call this API.
[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]));
}
}

Information Security SDK Request Collection Switch

Information Security SDK request collection switch, default is disabled, call this API to enable.

  • Related API
/**
* @brief Enable Information Security SDK request monitoring, default is disabled
* @note
* Tingyun SDK monitors Information Security SDK requests by intercepting the API that Information Security SDK uses to send requests to obtain request performance data. If Information Security SDK's request API is updated (method name changes or parameters change), it may cause interception to fail, and in severe cases may cause crashes. Therefore, after enabling this feature or updating Information Security SDK, testing should be performed.
*/
+ (void)startIMSecurityMonitor;

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

Support Collecting libcurl Request Data

Get libcurl request performance data, such as: IP, DNS, TCP, SSL and other performance metrics.
  • Related API
/**
* for record curl network.
*/
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 API, SDK will collect request headers, response headers, request body, and response body of Http/Https requests.

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

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

Note: Request content collection functionality is only enabled when platform "Collect Network Request Content" is enabled and this API is called.

  • Related API
/**
* Enable request content collection
*/
+ (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/Request Body Collection

After calling this API, you can custom set the request header/request body information to be collected. Multiple calls will override.

Note: If both platform and API configure request header collection, API configuration takes priority.

  • Related API
/**
* Set custom request header and request body collection 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 request URL contains www.tingyun.com, don't collect traceparent, traceid these two request headers
    if([url.absoluteString containsString:@"www.tingyun.com"])
    {
    NSMutableDictionary *dict = [allHTTPHeaderFields mutableCopy];
    [dict removeObjectsForKeys:@[@"traceparent",@"traceid"]];
    // Return custom collected request headers
    return dict;
    }
    // Return all request headers
    return allHTTPHeaderFields;
    }];
    }];
    • Request Body Collection
    [NBSAppAgent setupRequestSanitizer:^(NBSRequestSanitizer *requestSanitizer) {
    [requestSanitizer setRequestBodySanitizionWithBlock:^NSString * _Nullable(NSURL * _Nonnull url, NSDictionary * _Nonnull allHTTPHeaderFields, NSData * _Nonnull body) {
    // When request URL contains 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;
    }];

    }];

Request headers and request body can be configured together.

Custom Response Header/Response Body Collection

After calling this API, you can custom set the response header/response body information to be collected. Multiple calls will override.

Note: If both platform and API configure response header collection, API configuration takes priority.

  • API
/**
* Set custom response header and response body collection method
*/
+ (void)setupReponseSanitizer:(void (^)(NBSResponseSanitizer *responseSanitizer))configureSanitizer;
  • Example

    • Response Header Collection
    [NBSAppAgent setupReponseSanitizer:^(NBSResponseSanitizer *responseSanitizer) {
    [responseSanitizer setResponseHeaderSanitizionWithBlock:^NSDictionary * _Nullable(NSHTTPURLResponse * _Nonnull response) {
    // Set when request URL contains www.tingyun.com, don't collect x-Request-Id, Tracecode response headers
    if( [response.URL.absoluteString containsString:@"www.tingyun.com"]){
    NSMutableDictionary *dict = [response.allHeaderFields mutableCopy];
    [dict removeObjectsForKeys:@[@"x-Request-Id",@"Tracecode"]];
    // Return custom collected response headers
    return dict;
    }
    // Return all response headers
    return response.allHeaderFields;
    }];
    }];
    • Response Body Collection
        [NBSAppAgent setupReponseSanitizer:^(NBSResponseSanitizer *responseSanitizer) {

    [responseSanitizer setResponseBodySanitizionWithBlock:^NSString * _Nullable(NSHTTPURLResponse * _Nonnull response, NSData * _Nonnull body) {
    // When request URL contains 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;
    }];

    }];

Response headers and response body can be configured together.