• /
  • EnglishEspañol日本語한국어Português
  • Inicia sesiónComenzar ahora

Track HTTP requests

Syntax

Java

NewRelic.noticeHttpTransaction(string $url, string $httpMethod, int $statusCode, long $startTime, long $endTime, long $bytesSent, long $bytesReceived [, string $responseBody])

Kotlin

NewRelic.noticeHttpTransaction(
url: String?,
httpMethod: String?,
statusCode: Int,
startTimeMs: Long,
endTimeMs: Long,
bytesSent: Long,
bytesReceived: Long,
responseBody: String?
)

Description

Record HTTP transactions with an option to also send a response body.

If a network request fails, you can record details about the failure with noticeNetworkFailure().

Parameters

Parameter

Type

Description

$url

string

Required. The URL of the request.

$httpMethod

string

Required. The HTTP method used, such as GET or POST.

$statusCode

int

Required. The statusCode of the HTTP response, such as 200 for OK.

$startTime

int

Required. The start time of the request in milliseconds since the epoch.

$endTime

int

Required. The end time of the request in milliseconds since the epoch.

$bytesSent

int

Required. The number of bytes sent in the request.

$bytesReceived

int

Required. The number of bytes received in the response.

$responseBody

string

Optional. The response body of the HTTP response. The response body will be truncated and included in an HTTP Error metric if the HTTP transaction is an error.

Examples

Here's an example of tracking an HTTP transaction:

Java

public class CustomHttpMetricsLogger implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
// collect request start time
long t1 = System.nanoTime();
// get the size of the request body
long requestSize = null == request.body() ? 0 : request.body().contentLength();
// proceed with the request
Response response = chain.proceed(request);
// capture the time when response returns
long t2 = System.nanoTime();
long responseSize = null == response.body() ? 0 : response.body().contentLength();
// tell New Relic to notice this request
NewRelic.noticeHttpTransaction(request.urlString(), request.method(), response.code(), t1, t2, requestSize, responseSize);
// return response for processing
return response;
}
}

Kotlin

class CustomInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val t1 = System.nanoTime()
val aRequest: Request = chain.request()
val requestSize: Long = if (null == aRequest.body) 0 else aRequest.body!!.contentLength()
val aResponse = chain.proceed(aRequest)
val t2 = System.nanoTime()
val responseSize: Long = if (null == aResponse.body) 0 else aResponse.body!!.contentLength()
NewRelic.noticeHttpTransaction(aRequest.url.toString(), aRequest.method, aResponse.code, t1, t2, requestSize, responseSize)
return aResponse
}
}

Syntax

Objective-c

+ (void)noticeNetworkRequestForURL:(NSURL*)url
httpMethod:(NSString*)httpMethod
withTimer:(NRTimer*)timer
responseHeaders:(NSDictionary*)headers
statusCode:(NSInteger)httpStatusCode
bytesSent:(NSUInteger)bytesSent
bytesReceived:(NSUInteger)bytesReceived
responseData:(NSData *)responseData
traceHeaders:(NSDictionary<NSString*,NSString*>* _Nullable)traceHeaders
andParams:(NSDictionary * _Nullable)params;

Swift

func noticeNetworkRequest(for: URL, httpMethod: String, with: NRTimer, responseHeaders: [AnyHashable : Any], statusCode: Int, bytesSent: UInt, bytesReceived: UInt, responseData: Data, traceHeaders: [String : String], andParams: [AnyHashable : Any])

Description

New Relic will track the URL, response time, status code, and data sent and received.

If the response header's dictionary contains a X-NewRelic-AppData header, New Relic will track the association between the mobile app and the web server, and the New Relic UI will display the correlation and comparison of server vs. network vs. queue time.

If the HTTP status code indicates an error (400 and above), New Relic will also track this request as an error. The request header's dictionary and the response body data will be encoded as a server error in the New Relic UI.

Parameters

Parameter

Type

Description

$url

NSURL

Required. The URL of the request.

$httpMethod

string

Required. The HTTP method of the request.

$timer

NRTimer

Required. A timer that captures the start and end of the request.

$startTime

double

Optional. A double that captures the end time of the request. startTime and endTime can be used as an alternative to timer.

$endTime

double

Optional. A double that captures the end time of the request. (startTime and endTime can be used as an alternative to timer)

$responseHeaders

NSDictionary

Required. A dictionary of the headers returned in the server response.

$httpStatusCode

NSUInteger

Required. The status code of the HTTP response.

$bytesSent

NSUInteger

Required. The number of bytes sent in the request body.

$bytesReceived

NSUInteger

Required. The number of bytes received in the response body.

$responseData

NSData

Required. The response body data returned by the server. Used when recording a traced server error.

$traceHeaders

NSDictionary

Nullable. Used for distributed tracing.

$params

NSDictionary

Nullable. Unused.

Examples

Objective-C

Here's an example of tracking a HTTP transaction:

[NewRelic noticeNetworkRequestForURL:[NSURL URLWithString:@"https://www.newrelic.com"] httpMethod:@"GET" withTimer:[[NRTimer alloc] init]
responseHeaders:@{} statusCode:200 bytesSent:1024 bytesReceived:52
responseData:[NSData data] traceHeaders:nil andParams:nil];

Here's an example with start and end times:

[NewRelic noticeNetworkRequestForURL:[NSURL URLWithString:@"https://www.newrelic.com"] httpMethod:@"GET" startTime:0.0 endTime:0.1
responseHeaders:@{} statusCode:200 bytesSent:1024 bytesReceived:52
responseData:[NSData data] traceHeaders:nil andParams:nil];

Swift

Here's an example of tracking a HTTP transaction:

NewRelic.noticeNetworkRequest(for: URL(string: "https://www.newrelic.com"), httpMethod: "GET", with: NRTimer(), responseHeaders: [:],
statusCode: 200, bytesSent: 1000, bytesReceived: 1000, responseData: Data(), traceHeaders: nil, andParams: nil)

Here's an example with start and end times:

NewRelic.noticeNetworkRequest(for: URL(string: "https://www.newrelic.com"), httpMethod: "GET", startTime: 0.0, endTime: 0.1, responseHeaders: [:],
statusCode: 200, bytesSent: 1000, bytesReceived: 1000, responseData: Data(), traceHeaders: nil, andParams: nil)

Syntax

noticeHttpTransaction(options: {
url: string,
method: string,
status: number,
startTime: number,
endTime: number,
bytesSent: number,
bytesReceived: number,
body: string,
}) => void;

Description

Manually records HTTP transactions, with an option to also send a response body.

Parameters

Parameter

Type

Description

url

string

Required. The URL of the request.

method

string

Required. The HTTP method of the request.

status

number

Required. The HTTP status code of the response.

startTime

number

Optional. The start time of the request in milliseconds since the epoch. startTime and endTime can be used as an alternative to timer.

endTime

number

Optional. The end time of the request in milliseconds since the epoch. startTime and endTime can be used as an alternative to timer.

bytesSent

number

Required. The number of bytes sent in the request.

bytesReceived

number

Required. The number of bytes received in the response.

body

string

Optional. The response body of the HTTP response.

Example

NewRelicCapacitorPlugin.noticeHttpTransaction({
url: "https://fakewebsite.com",
method: "GET",
status: 200,
startTime: Date.now(),
endTime: Date.now(),
bytesSent: 10,
bytesReceived: 2500,
body: "fake http response body 200",
});

Syntax

noticeHttpTransaction(url: string, method: string, status: number, startTime: number, endTime: number, bytesSent: number, bytesReceived: number, body?: string)

Description

The New Relic Cordova plugin automatically collects HTTP transactions, but if you want to manually record HTTP transactions, use this method to do so.

Parameters

Parameter

Type

Description

url

string

Required. The URL of the request.

method

string

Required. The HTTP method of the request.

status

number

Required. The HTTP status code of the response.

startTime

number

Optional. The start time of the request in milliseconds since the epoch. startTime and endTime can be used as an alternative to timer.

endTime

number

Optional. The end time of the request in milliseconds since the epoch. startTime and endTime can be used as an alternative to timer.

bytesSent

number

Required. The number of bytes sent in the request.

bytesReceived

number

Required. The number of bytes received in the response.

body?

string

Optional. The response body of the HTTP response.

Example

NewRelic.noticeHttpTransaction('https://fakewebsiteurl.com', 'GET', 200, Date.now(), Date.now(), 0, 100000, 'fake request body');

Syntax

NoticeHttpTransaction(string url, string httpMethod, int statusCode, long startTime,long endTime, long bytesSent, long bytesReceived, string responseBody): void;

Description

Tracks network requests manually. You can use this method to record HTTP transactions, with an option to also send a response body.

Parameters

Parameter

Type

Description

url

string

Required. The URL of the request.

httpmethod

string

Required. The HTTP method of the request.

statusCode

int

Required. The HTTP status code of the response.

startTime

long

Optional. The start time of the request in milliseconds since the epoch. startTime and endTime can be used as an alternative to timer.

endTime

long

Optional. The end time of the request in milliseconds since the epoch. startTime and endTime can be used as an alternative to timer.

bytesSent

long

Required. The number of bytes sent in the request.

bytesReceived

long

Required. The number of bytes received in the response.

responseBody

string

Optional. The response body of the HTTP response.

Example

CrossNewRelic.Current.NoticeHttpTransaction(
"https://newrelic.com",
"GET",
200,
DateTimeOffset.Now.ToUnixTimeMilliseconds(),
DateTimeOffset.Now.ToUnixTimeMilliseconds() + 100,
0,
1000,
""
);

Syntax

noticeHttpTransaction(String url,String httpMethod,int statusCode,int startTime,int endTime,int bytesSent,int bytesReceived,Map<String, dynamic>? traceData,{String responseBody = ""}): void;

Description

Tracks network requests manually. You can use this method to record HTTP transactions, with an option to also send a response body.

Parameters

Parameter

Type

Description

url

string

Required. The URL of the request.

httpmethod

string

Required. The HTTP method of the request.

statusCode

int

Required. The HTTP status code of the response.

startTime

int

Optional. The start time of the request in milliseconds since the epoch. startTime and endTime can be used as an alternative to timer.

endTime

int

Optional. The end time of the request in milliseconds since the epoch. startTime and endTime can be used as an alternative to timer.

bytesSent

int

Required. The number of bytes sent in the request.

bytesReceived

int

Required. The number of bytes received in the response.

traceData

dictionary

Optional. A dictionary of key-value pairs that can be used to provide additional information about the transaction.

responseBody

string

Optional. The response body of the HTTP response.

Example

NewrelicMobile.instance.noticeHttpTransaction("https://cb6b02be-a319-4de5-a3b1-361de2564493.mock.pstmn.io/searchpage", "GET",200, 1000, 2000,100,300,null,responseBody: "This is Test Payload");

Syntax

noticeHttpTransaction(url: string, httpMethod: string, statusCode: number, startTime: number, endTime: number, bytesSent: number, bytesReceived: number, responseBody: string): void;

Description

Tracks network requests manually. You can use this method to record HTTP transactions, with an option to also send a response body.

Parameters

Parameter

Type

Description

url

string

Required. The URL of the request.

httpmethod

string

Required. The HTTP method of the request.

statusCode

number

Required. The HTTP status code of the response.

startTime

number

Optional. The start time of the request in milliseconds since the epoch. startTime and endTime can be used as an alternative to timer.

endTime

number

Optional. The end time of the request in milliseconds since the epoch. startTime and endTime can be used as an alternative to timer.

bytesSent

number

Required. The number of bytes sent in the request.

bytesReceived

number

Required. The number of bytes received in the response.

responseBody

string

Optional. The response body of the HTTP response.

Example

NewRelic.noticeHttpTransaction('https://github.com', 'GET', 200, Date.now(), Date.now()+1000, 100, 101, "response body");

Syntax

noticeHttpTransaction(string url, string httpMethod, int statusCode, long startTime,long endTime, long bytesSent, long bytesReceived, string responseBody): void;

Description

Tracks network requests manually. You can use this method to record HTTP transactions, with an option to also send a response body.

Parameters

Parameter

Type

Description

url

string

Required. The URL of the request.

httpmethod

string

Required. The HTTP method of the request.

statusCode

int

Required. The HTTP status code of the response.

startTime

long

Optional. The start time of the request in milliseconds since the epoch. startTime and endTime can be used as an alternative to timer.

endTime

long

Optional. The end time of the request in milliseconds since the epoch. startTime and endTime can be used as an alternative to timer.

bytesSent

long

Required. The number of bytes sent in the request.

bytesReceived

long

Required. The number of bytes received in the response.

responseBody

string

Optional. The response body of the HTTP response.

Example

NewRelicAgent.NoticeHttpTransaction(
"https://github.com",
"GET",
200,
DateTimeOffset.Now.ToUnixTimeMilliseconds(),
DateTimeOffset.Now.ToUnixTimeMilliseconds() + 1000,
100,
101,
"response body",
null
);

Syntax

NoticeHttpTransaction(string url, string httpMethod, int statusCode, long startTime,long endTime, long bytesSent, long bytesReceived, string responseBody): void;

Description

Tracks network requests manually. You can use this method to record HTTP transactions, with an option to also send a response body.

Parameters

Parameter

Type

Description

url

string

Required. The URL of the request.

httpmethod

string

Required. The HTTP method of the request.

statusCode

int

Required. The HTTP status code of the response.

startTime

long

Optional. The start time of the request in milliseconds since the epoch. startTime and endTime can be used as an alternative to timer.

endTime

long

Optional. The end time of the request in milliseconds since the epoch. startTime and endTime can be used as an alternative to timer.

bytesSent

long

Required. The number of bytes sent in the request.

bytesReceived

long

Required. The number of bytes received in the response.

responseBody

string

Optional. The response body of the HTTP response.

Example

CrossNewRelicClient.Current.NoticeHttpTransaction(
"https://newrelic.com",
"GET",
200,
DateTimeOffset.Now.ToUnixTimeMilliseconds(),
DateTimeOffset.Now.ToUnixTimeMilliseconds() + 100,
0,
1000,
""
);
Copyright © 2024 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.