Home > Blockchain >  Where do I add my user-agent information and how?
Where do I add my user-agent information and how?

Time:02-03

I am building a rest API in ASP.NET Core 6.0

I am using several API's for a mashup, in which one of them demands that I set a "meaningful" user-agent. Can someone explain where (in which class or something) in the project I put the HttpRequest.UserAgent property and how exactly. I've been trying to search for a good explanation but it doesn't make sense to me.

the api I'm using is musicbrainz, in which i want the user-agent to look something like this:

MyAwesomeTagger/1.2.0 ( [email protected] )

Thank you for any help.

CodePudding user response:

Thank you for a quick response in the comments - does this look right?

        {
              using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("ApiMashupname/6.0", "[email protected]");
                var response = await client.GetAsync(endpoint, HttpCompletionOption.ResponseHeadersRead);
                response.EnsureSuccessStatusCode();
                var data = await response.Content.ReadAsStringAsync();
                var result = JsonSerializer.Deserialize<T>(data);
                return result;
            }

        }

CodePudding user response:

You should add user agent in UserAgent property of DefaultRequestHeader

client.DefaultRequestHeaders.UserAgent.ParseAdd("ApiMashupname/6.0([email protected])");
  •  Tags:  
  • Related