skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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;
                }
    
            }
    

  2. You should add user agent in UserAgent property of DefaultRequestHeader

    client.DefaultRequestHeaders.UserAgent.ParseAdd("ApiMashupname/6.0([email protected])");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search