---
title: "HTTP Integration"
description: "Learn more about the Sentry HTTP integration for the Dart SDK."
url: https://docs.sentry.io/platforms/dart/integrations/http-integration/
---

# HTTP Integration | Sentry for Dart

References to "transactions" on this page apply to the default transaction mode. In stream mode, this integration creates service spans instead, and Sentry sends them as they finish. See [Streamed Spans](https://docs.sentry.io/platforms/dart/tracing/streamed-spans.md) for more information.

## [Using the `SentryHttpClient` function](https://docs.sentry.io/platforms/dart/integrations/http-integration.md#using-the-sentryhttpclient-function)

You can use the `SentryHttpClient` and call its methods directly, or you can use it with the [`runWithClient`](https://pub.dev/documentation/http/latest/http/runWithClient.html) function. If you need to use a fresh instance of the `client` (so that other instances are not affected) or to run in a separate [`Zone`](https://api.dart.dev/stable/3.5.0/dart-async/Zone-class.html), which allows you to override the functionality of the existing [`Zone`](https://api.dart.dev/stable/3.5.0/dart-async/Zone-class.html), then use the `runWithClient` function (see the `runWithClient` tab). Otherwise, just use `SentryHttpClient` directly and call its methods (see the `default` tab).

### [Usage](https://docs.sentry.io/platforms/dart/integrations/http-integration.md#usage)

**\[default] http\_client.dart**

```dart
import 'package:sentry/sentry.dart';

final client = SentryHttpClient();

try {
  final response = await client.get(Uri.https('www.example.com', ''));
  print(response.body);
} finally {
  client.close();
}
```

**\[runWithClient] http\_client.dart**

```dart
import 'package:http/http.dart';
import 'package:sentry/sentry.dart';

final sentryHttpClient = SentryHttpClient();

await runWithClient(() async {
  final response = await get(Uri.https('www.example.com', ''));
  print(response.body);
}, () => sentryHttpClient);
```

## [Reporting Bad HTTP Requests as Errors](https://docs.sentry.io/platforms/dart/integrations/http-integration.md#reporting-bad-http-requests-as-errors)

The `SentryHttpClient` can also catch exceptions that may occur during requests — for example [`SocketException`](https://api.dart.dev/stable/2.13.4/dart-io/SocketException-class.html).

```dart
import 'package:sentry/sentry.dart';

var client = SentryHttpClient();
try {
  var uriResponse = await client.post('https://example.com/whatsit/create',
    body: {'name': 'doodle', 'color': 'blue'});
  print(await client.get(uriResponse.bodyFields['uri']));
} finally {
  client.close();
}
```

When an error occurs, the following information is captured and sent to Sentry:

The marked elements (\*) are affected by the default enabled [server-side data scrubbing](https://docs.sentry.io/security-legal-pii/scrubbing/server-side-scrubbing.md). To implement client side data scrubbing, go to [client-side data scrubbing in Flutter](https://docs.sentry.io/platforms/dart/guides/flutter/data-management/sensitive-data.md).

Request details:

* Method
* URL \*
* Headers \*
* Body \*
* Content length
* Duration

Response details:

* Headers \*
* Content length
* Status code

This is an opt-out feature. The following example shows how to disable it:

```dart
import 'package:sentry/sentry.dart';

Future<void> main() async {
  await Sentry.init(
    (options) {
      options.dsn = 'https://<key>@o<orgId>.ingest.sentry.io/<projectId>';
      options.captureFailedRequests = false;
    },
    appRunner: initApp, // Init your App.
  );
}
```

Furthermore you can track HTTP requests that you consider bad. In the following example, exceptions are captured for each request with a status code within the range of 400 to 404, and also for 500.

```dart
import 'package:sentry/sentry.dart';

var client = SentryHttpClient(
  failedRequestStatusCodes: [
    SentryStatusCode.range(400, 404),
    SentryStatusCode(500),
  ],
);

try {
  var uriResponse = await client.post('https://example.com/whatsit/create',
    body: {'name': 'doodle', 'color': 'blue'});
  print(await client.get(uriResponse.bodyFields['uri']));
} finally {
  client.close();
}
```

### [Failed Request Status Codes](https://docs.sentry.io/platforms/dart/integrations/http-integration.md#failed-request-status-codes)

You can customize which status codes should be considered failed requests by setting the `failedRequestStatusCodes` option when calling `SentryHttpClient()`.

```dart
import 'package:sentry/sentry.dart';

var client = SentryHttpClient(
  failedRequestStatusCodes: [
    SentryStatusCode.range(400, 404), // Capture 400-404
    SentryStatusCode(500),             // Capture 500
  ],
);

try {
  var uriResponse = await client.post('https://example.com/whatsit/create',
    body: {'name': 'doodle', 'color': 'blue'});
  print(await client.get(uriResponse.bodyFields['uri']));
} finally {
  client.close();
}
```

**Default Behavior:**

By default, `failedRequestStatusCodes` is set to `[SentryStatusCode.range(500, 599)]`, which captures server errors (status codes 500-599).

### [Failed Request Targets](https://docs.sentry.io/platforms/dart/integrations/http-integration.md#failed-request-targets)

To control which URLs should have failed requests captured, use the `failedRequestTargets` option. This is useful when you only want to capture errors from specific APIs or domains.

The SDK will only capture HTTP client errors if the request URL matches one of the provided targets. Targets can be:

* Strings that appear anywhere in the URL
* Regular expression patterns

```dart
import 'package:sentry/sentry.dart';

// Capture failed requests only from specific domains
var client = SentryHttpClient(
  failedRequestTargets: [
    'api.example.com',      // Matches any URL containing this string
    'myapi.com',            // Another domain to track
  ],
);

try {
  var uriResponse = await client.post('https://api.example.com/whatsit/create',
    body: {'name': 'doodle', 'color': 'blue'});
  print(await client.get(uriResponse.bodyFields['uri']));
} finally {
  client.close();
}
```

**Default Behavior:**

By default, `failedRequestTargets` is set to `['.*']`, which matches all URLs. This means all failed requests are captured (subject to `failedRequestStatusCodes`).

## [Tracing for HTTP Requests](https://docs.sentry.io/platforms/dart/integrations/http-integration.md#tracing-for-http-requests)

Capturing transactions requires that you first [set up tracing](https://docs.sentry.io/platforms/dart/tracing.md) if you haven't already.

The `SentryHttpClient` starts a span off the active span for each HTTP request. In transaction mode, that's the transaction bound to the scope; in stream mode, it's the span started with `Sentry.startSpan()`.

**Transaction Mode (Default)**

```dart
import 'package:sentry/sentry.dart';

final transaction = Sentry.startTransaction(
  'webrequest',
  'request',
  bindToScope: true,
);

var client = SentryHttpClient();
try {
  var uriResponse = await client.post('https://example.com/whatsit/create',
    body: {'name': 'doodle', 'color': 'blue'});
  print(await client.get(uriResponse.bodyFields['uri']));
} finally {
  client.close();
}

await transaction.finish(status: SpanStatus.ok());
```

**Stream Mode**

```dart
import 'package:sentry/sentry.dart';

// Start a root span — a span is attached for each HTTP request
await Sentry.startSpan('webrequest', (span) async {
  var client = SentryHttpClient();
  try {
    var uriResponse = await client.post('https://example.com/whatsit/create',
      body: {'name': 'doodle', 'color': 'blue'});
    print(await client.get(uriResponse.bodyFields['uri']));
  } finally {
    client.close();
  }
}, parentSpan: null);
```
