Headers, X-NetSapiens-Log, and InSight on-demand logging
Using specified header to log requests
In this guide we'll be documenting the use of the X-NetSapiens-Log header in order to collect real-time on-demand logging data from InSight during API testing. Let's briefly cover what this header is, though. API headers are key-value pairs sent with HTTP requests that provide important metadata about the request or response.
Headers serve several crucial functions in API requests:
Authentication and Authorization: Headers often carry credentials that prove who you are and what you're allowed to do. The most common pattern is the Authorization header:
// Bearer token authentication like our API uses
fetch('/api/endpoint', {
headers: {
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
}
});
Content Negotiation: Headers tell the server what kind of data you're sending and what you expect back. The Content-Type and Accept headers handle this:
fetch('/api/endpoint', {
headers: {
'Content-Type': 'application/json', // The data we're sending is JSON
'Accept': 'application/json' // We want JSON back
},
body: JSON.stringify({ name: 'John' })
});
Custom Behavior Control: Custom headers (usually prefixed with X-) can trigger special behaviors. This is where X-NetSapiens-Log comes in:
fetch('/api/users', {
headers: {
'X-NetSapiens-Log': 'some tag name', // Enable detailed logging for this request with a custom tag
'X-Request-ID': '123e4567-e89b', // E.G. Track this request through the system
}
});
On-Demand Logging and X-NetSapiens-Log Header
Next we'll describe the process of using the X-NetSapiens-Log header for your API testing. What follows is an example flow:
- Create an API request for the netsapiens-api package, e.g.:
curl --location --request POST 'https://www.example.com/ns-api/oauth2/token/?grant_type=password&client_id=xxxxx&client_secret=xxxxxxxxxxxxxxxxxxxx&username=1099%40xxxxxxx&password=Xxxxxxx%3F' \
- Add the header for On-Demand logs(X-NetSapiens-Log) to the API request:
curl --location --request POST 'https://qa-u22-tor10.netsapiens.com/ns-api/oauth2/token/?grant_type=password&client_id=xxxxx&client_secret=xxxxxxxxxxxxxxxxxxxx&username=1099%40xxxxxxx&password=Xxxxxxx%3F' \ --header 'X-NetSapiens-Log: LOGTAG'
- ...where
LOGTAG
is any string you want to use to search and track on
- Execute the API request through whichever tool or interface you're using.
- Go to iNSight application.
- Select the appropriate organization where the servers is added.
- Click on Search Dashboards.
- Search for 'On-demand API and Portal Logs'.
- Select the dashboard for OnDemand logs.
- Enter the value for X-NetSapiens-Log under
search value1
, like so:-
example
-
At this point you will begin to see your API requests be logged there in the InSight panel in real time.
Updated 1 day ago