0

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 ( me@example.com )

Thank you for any help.

stersym
  • 308
  • 1
  • 6
  • 15
  • How are you trying to consume that 'musicbrainz' API? – J.Salas Jan 27 '22 at 09:12
  • I am using the API root URL to get the release-group of a specific mbId. – awkward_coder Jan 27 '22 at 09:16
  • 2
    Does this answer your question? [Custom User Agent for HttpClient?](https://stackoverflow.com/questions/13772937/custom-user-agent-for-httpclient) – msmolcic Jan 27 '22 at 09:20
  • 1
    Does this answer your question? [How do I set a default User Agent on an HttpClient?](https://stackoverflow.com/questions/44076962/how-do-i-set-a-default-user-agent-on-an-httpclient) – Anand Sowmithiran Jan 27 '22 at 09:20

2 Answers2

0

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

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

        }
0

You should add user agent in UserAgent property of DefaultRequestHeader

client.DefaultRequestHeaders.UserAgent.ParseAdd("ApiMashupname/6.0(emailaddress@hotmail.com)");