Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 25, 2025

Exposes the HTTP protocol version (1.0, 1.1, 2.0) in Response.extra for telemetry/observability use cases like OTEL.

Changes

  • dio/lib/src/adapter.dart: Add httpVersionKey constant for accessing protocol version from Response.extra
  • dio/lib/src/adapters/io_adapter.dart: Extract protocolVersion from internal _HttpHeaders via dynamic access (not in public API), with graceful fallback for mock environments
  • plugins/http2_adapter/lib/src/http2_adapter.dart: Set httpVersionKey = '2.0' for HTTP/2 connections

Usage

final response = await dio.get('https://example.com');
final httpVersion = response.extra[httpVersionKey]; // "1.0", "1.1", or "2.0"

Note: Web platform (BrowserHttpClientAdapter) does not expose protocol version via XMLHttpRequest.

New Pull Request Checklist

  • I have read the Documentation
  • I have searched for a similar pull request in the project and found none
  • I have updated this branch with the latest main branch to avoid conflicts (via merge from master or rebase)
  • I have added the required tests to prove the fix/feature I'm adding
  • I have updated the documentation (if necessary)
  • I have run the tests without failures
  • I have updated the CHANGELOG.md in the corresponding package

Additional context and info (if any)

Closes #2298. The protocolVersion property exists on Dart's internal _HttpHeaders class but is not exposed in the public HttpHeaders interface, requiring dynamic access.

Original prompt

This section details on the original issue you should resolve

<issue_title>Indicate http version used for connection/response</issue_title>
<issue_description>### Request Statement

When receiving an http response from upstream server it would be really handy to know which http version served the response. E.g. http1.1 / http2

This is super handy to add to e.g. OTEL telemetry to understand how the responses are being server from upstream infrastructure.

Thank you!

Solution Brainstorm

Updating the ResponseBody with an extra parameter might be an ok place to put this. Its not really the body of the response, but it already has some non-body items in anyway....</issue_description>

<agent_instructions>Investigate if it's truly possible to identify the actual HTTP protocol.
If yes, file a PR to implement it; If not, reply to the issue instead.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@AlexV525 Based on my knowledge, the *protocol* header with the HTTP version should be available in the response's header. @AlexV525 For HTTP/1.x it's available in the raw header, but it's not publicly visible. Image

For H2 we have nothing available from the transport, unfortunately.

We seem to have no reliable method to inspect the protocol during the request and response. WDYT?</comment_new>
<comment_new>@AlexV525

The http2 proto is handled by a separate code path, so it already knows that it is http2..

and http2 (h2) can be hardcoded in the http2 handler? If the status line protocol version isn't available at all, it would still be ok to make distinction between http1.x and 2, as there are different code paths.

If you don't access the adapter directly, you won't know if it's actually h2:

} on DioH2NotSupportedException catch (e) {
// Fallback to use the callback
// or to another adapter (typically IOHttpClientAdapter)
// since the request can have a better handle by it.
if (onNotSupported != null) {
return onNotSupported!(options, requestStream, cancelFuture, e);
}
return fallbackAdapter.fetch(options, requestStream, cancelFuture);

The adapter will automatically retry with H1 if H2 is not available.
So the result here doesn't make sense since it's just simply guessing.

Can get the http1.x proto from status line - 99% it will be 1.1,

This is also inaccessible with dart:io AFAIK.

Then can be exposed in a way to make it public in the ResponseBody.

We intend to place only necessary information in classes. To retrieve the protocol version, based on the above paths, you can guess the protocol version from what adapter you are using, and make a minimum override to the H2 adapter to be aware of the fallback behavior.</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Add HTTP version indication to response Add HTTP protocol version to response extra field Nov 25, 2025
Copilot AI requested a review from AlexV525 November 25, 2025 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Indicate http version used for connection/response Documentation: Transformer example link broken

2 participants