Control how data is passed in network requests or responses
By default, the LogRocket React Native SDK captures all network requests made through XMLHttpRequests and the Javascript Fetch API.
Sanitize a network request
requestSanitizer - Function
requestSanitizer - Functionoptional
Use requestSanitizer to scrub sensitive data from network requests or to ignore request/response pairs.
To ignore a request/response pair return null from the requestSanitizer. You can also redact fields on the request object by modifying the object. Make sure that you still return the modified request from the function:
// Add this to your existing init call, do not call init twice!
LogRocket.init(YOUR_APP_ID, {
network: {
requestSanitizer: request => {
// if the url contains 'ignore'
if (request.url.toLowerCase().indexOf('ignore') !== -1) {
// ignore the request response pair
return null;
}
// otherwise log the request normally
return request;
},
},
});// Add this to your existing init call, do not call init twice!
LogRocket.init(YOUR_APP_ID, {
network: {
requestSanitizer: request => {
// if the url contains 'ignore'
if (request.url.toLowerCase().indexOf('ignore') !== -1) {
// scrub out the body
return {
...request,
body: null,
};
}
return request;
},
},
});// Add this to your existing init call, do not call init twice!
LogRocket.init(YOUR_APP_ID, {
network: {
requestSanitizer: request => {
return {
...request,
headers: {
...request.headers,
'x-auth-token': null,
},
};
},
},
});// Add this to your existing init call, do not call init twice!
LogRocket.init(YOUR_APP_ID, {
network: {
requestSanitizer: request => {
return {
...request,
headers: {
...request.headers,
'x-auth-token': '',
},
};
},
},
});Sanitize a network response
responseSanitizer - Function
responseSanitizer - Functionoptional
Use the responseSanitizer to redact sensitive data from network response. Return null from the function to redact all response fields and only record timing data.
// Add this to your existing init call, do not call init twice!
LogRocket.init(YOUR_APP_ID, {
network: {
responseSanitizer: (response) => {
if (response.body?.includes('<field to redact>')) {
return {
...response,
body: undefined,
};
}
return response;
},
},
});// Add this to your existing init call, do not call init twice!
LogRocket.init(YOUR_APP_ID, {
network: {
responseSanitizer: response => {
return null;
},
},
});if (response.body.**field_to_delete**) {
return {
...response,
body: {
...response.body,
**field_to_delete**: undefined,
},
};
}if (response.body.**field_to_hide**) {
return {
...response,
body: {
...response.body,
**field_to_hide**: '**redacted**',
},
};
}
return response
logrocket-fuzzy-search-sanitizerJoshua Bailey, a LogRocket user, contributed a plugin for more easily sanitizing data from network requests and responses. Check it out here: https://github.com/LogRocket/logrocket-fuzzy-search-sanitizer
Disable recording of network data
isEnabled - Boolean
isEnabled - Booleanoptional (default - true)
If you wish to disable all network data recording, you may set isEnabled to false.
// Add this to your existing init call, do not call init twice!
LogRocket.init(YOUR_APP_ID, {
network: {
isEnabled: false,
},
});
Sanitizing headers and bodies to maintain timing and error dataWhen possible, we recommend using the
requestSanitizerandresponseSanitizerto remove all headers and bodies, instead ofisEnabled: false. Sanitizing the headers and bodies specifically will allow LogRocket to still detect failed network requests, network errors, and slow network requests.
