|
1 | 1 | ---
|
2 | 2 | title: HTTP Integration
|
3 | 3 | description: "Learn more about the Sentry HTTP integration for the Dart SDK."
|
4 |
| -sidebar_order: 2 |
| 4 | +sidebar_order: 30 |
| 5 | +redirect_from: |
| 6 | + - /platforms/dart/guides/http/ |
5 | 7 | ---
|
6 | 8 |
|
7 |
| -## Using the `SentryHttpClient` function |
8 |
| - |
9 |
| -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). |
10 |
| - |
11 |
| -### Usage |
12 |
| - |
13 |
| -```dart {filename:http_client.dart}{tabTitle: default} |
14 |
| -import 'package:sentry/sentry.dart'; |
15 |
| -
|
16 |
| -final client = SentryHttpClient(); |
17 |
| -
|
18 |
| -try { |
19 |
| - final response = await client.get(Uri.https('www.example.com', '')); |
20 |
| - print(response.body); |
21 |
| -} finally { |
22 |
| - client.close(); |
23 |
| -} |
24 |
| -``` |
25 |
| - |
26 |
| -```dart {filename:http_client.dart}{tabTitle: runWithClient} |
27 |
| -import 'package:http/http.dart'; |
28 |
| -import 'package:sentry/sentry.dart'; |
29 |
| -
|
30 |
| -final sentryHttpClient = SentryHttpClient(); |
31 |
| -
|
32 |
| -await runWithClient(() async { |
33 |
| - final response = await get(Uri.https('www.example.com', '')); |
34 |
| - print(response.body); |
35 |
| -}, () => sentryHttpClient); |
36 |
| -``` |
37 |
| - |
38 |
| -## Reporting Bad HTTP Requests as Errors |
39 |
| - |
40 |
| -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). |
41 |
| - |
42 |
| -```dart |
43 |
| -import 'package:sentry/sentry.dart'; |
44 |
| -
|
45 |
| -var client = SentryHttpClient(); |
46 |
| -try { |
47 |
| - var uriResponse = await client.post('https://example.com/whatsit/create', |
48 |
| - body: {'name': 'doodle', 'color': 'blue'}); |
49 |
| - print(await client.get(uriResponse.bodyFields['uri'])); |
50 |
| -} finally { |
51 |
| - client.close(); |
52 |
| -} |
53 |
| -``` |
54 |
| - |
55 |
| -When an error occurs, the following information is captured and sent to Sentry: |
56 |
| - |
57 |
| -The marked elements (\*) are affected by the default enabled [server-side data scrubbing](https://docs.sentry.io/security-legal-pii/scrubbing/server-side-scrubbing/). |
58 |
| -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/). |
59 |
| - |
60 |
| -Request details: |
61 |
| - |
62 |
| -- Method |
63 |
| -- URL \* |
64 |
| -- Headers \* |
65 |
| -- Body \* |
66 |
| -- Content length |
67 |
| -- Duration |
68 |
| - |
69 |
| -Response details: |
70 |
| - |
71 |
| -- Headers \* |
72 |
| -- Content length |
73 |
| -- Status code |
74 |
| - |
75 |
| -This is an opt-out feature. The following example shows how to disable it: |
76 |
| - |
77 |
| - |
78 |
| -```dart |
79 |
| -import 'package:sentry/sentry.dart'; |
80 |
| -
|
81 |
| -Future<void> main() async { |
82 |
| - await Sentry.init( |
83 |
| - (options) { |
84 |
| - options.dsn = '___PUBLIC_DSN___'; |
85 |
| - options.captureFailedRequests = false; |
86 |
| - }, |
87 |
| - appRunner: initApp, // Init your App. |
88 |
| - ); |
89 |
| -} |
90 |
| -``` |
91 |
| - |
92 |
| -Furthermore you can track HTTP requests that you consider bad. In the |
93 |
| -following example, exceptions are captured for each request |
94 |
| -with a status code within the range of 400 to 404, and also for 500. |
95 |
| - |
96 |
| -```dart |
97 |
| -import 'package:sentry/sentry.dart'; |
98 |
| -
|
99 |
| -var client = SentryHttpClient( |
100 |
| - failedRequestStatusCodes: [ |
101 |
| - SentryStatusCode.range(400, 404), |
102 |
| - SentryStatusCode(500), |
103 |
| - ], |
104 |
| -); |
105 |
| -
|
106 |
| -try { |
107 |
| - var uriResponse = await client.post('https://example.com/whatsit/create', |
108 |
| - body: {'name': 'doodle', 'color': 'blue'}); |
109 |
| - print(await client.get(uriResponse.bodyFields['uri'])); |
110 |
| -} finally { |
111 |
| - client.close(); |
112 |
| -} |
113 |
| -``` |
114 |
| - |
115 |
| -## Tracing for HTTP Requests |
116 |
| - |
117 |
| -<Alert> |
118 |
| - |
119 |
| -Capturing transactions requires that you first <PlatformLink to="/tracing/">set up tracing</PlatformLink> if you haven't already. |
120 |
| - |
121 |
| -</Alert> |
122 |
| - |
123 |
| -The `SentryHttpClient` starts a span out of the active span bound to the scope for each HTTP Request. |
124 |
| - |
125 |
| -```dart |
126 |
| -import 'package:sentry/sentry.dart'; |
127 |
| -
|
128 |
| -final transaction = Sentry.startTransaction( |
129 |
| - 'webrequest', |
130 |
| - 'request', |
131 |
| - bindToScope: true, |
132 |
| -); |
133 |
| -
|
134 |
| -var client = SentryHttpClient(); |
135 |
| -try { |
136 |
| - var uriResponse = await client.post('https://example.com/whatsit/create', |
137 |
| - body: {'name': 'doodle', 'color': 'blue'}); |
138 |
| - print(await client.get(uriResponse.bodyFields['uri'])); |
139 |
| -} finally { |
140 |
| - client.close(); |
141 |
| -} |
142 |
| -
|
143 |
| -await transaction.finish(status: SpanStatus.ok()); |
144 |
| -``` |
| 9 | +<Include name="dart-integrations/http-integration.mdx" /> |
0 commit comments