question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Cannot set `User-Agent` header in track2 SDK

See original GitHub issue

Library name and version

Azure.ResourceManager

Describe the bug

Description

The following code snippet (copied from https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Pipeline.md#implementing-a-synchronous-policy) doesn’t work.

public class CustomRequestPolicy : HttpPipelineSynchronousPolicy
{
    public override void OnSendingRequest(HttpMessage message)
    {
        message.Request.Headers.Add("User-Agent", "abc");
    }
}

The root cause is that we have an internal override https://github.com/Azure/azure-sdk-for-net/blob/d269eede20a23edec101477601a2b90bee54e690/sdk/resourcemanager/Azure.ResourceManager/src/Internal/MgmtTelemetryPolicy.cs#L21 So external users cannot simply set User-Agent by Headers.Add() or Headers.Set().

Expected behavior

The following code snippet should add a User-Agent header value.

public class CustomRequestPolicy : HttpPipelineSynchronousPolicy
{
    public override void OnSendingRequest(HttpMessage message)
    {
        message.Request.Headers.Add("User-Agent", "abc");
    }
}

Actual behavior

User-Agent header value cannot be modified through Headers.Add() or Headers.Set().

Reproduction Steps

Try the code snippet at the beginning.

Environment

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
archerzzcommented, Jan 17, 2022

I think we need to make a decision: shall we allow users to modify User-Agent? We could change the logic in MgmtTelemetryPolicy.cs to add (v.s. set) the value passed by UserAgentOverride, which will make sure the User-Agent set by each SDK will be passed to back-end.

1reaction
archerzzcommented, Jan 24, 2022

I checked with Powershell team. And it looks like the backend analytics can support long strings. So appending custom user-agent headers seems the best option, e.g. the final header value will look like:

azsdk-net-ResourceManager.XXX/1.0.0-alpha.20220124.1 (.NET Framework 4.8.4420.0; Microsoft Windows 10.0.19043 ) AzurePowershell/v6.6.0 Az.KeyValue/4.1.0 PSVersion/v7.1.3

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sending a custom User-Agent string along with my ...
1 Answer. After some testing, Chrome does indeed have a bug with the User-Agent header. This is most likely due to the fact...
Read more >
Topics with Label: Possible Bug
Refused to set unsafe header "user-agent".
Read more >
KinesisVideoArchivedMediaClient
KinesisVideoArchivedMediaClient Operations · x-amz-ErrorType HTTP header – contains a more specific error type in addition to what the HTTP status code provides.
Read more >
User-Agent header disallowed via 'Access-Control-Allow- ...
I've started making a web app that interacts with the iNaturalist API directly through javascript in the browser, and I've been trying to...
Read more >
Am I breaking the API rules if I don't set the User-Agent ...
I am developing a HTML + Javascript application that uses the reddit API. The app obtains the OAuth access token using the implicit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found