Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DEV: Add a user agent to all HTTP requests that Discourse makes. #31555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 3, 2025

Conversation

pento
Copy link
Member

@pento pento commented Feb 28, 2025

✨ What's This?

This change monkey-patches Net::HTTPHeader to add a default User-Agent header to all requests that Discourse makes.

πŸ‘‘ Testing

You can use a HTTP echo service to test that the header is being set correctly. For example:

% rails c
Loading development environment (Rails 7.2.2.1)
[1] pry(main)> JSON.parse(Net::HTTP.get(URI("https://echo.free.beeceptor.com")))
=> {"method"=>"GET",
 "protocol"=>"https",
 "host"=>"echo.free.beeceptor.com",
 "path"=>"/",
 "ip"=>"...",
 "headers"=>
  {"Host"=>"echo.free.beeceptor.com",
   "User-Agent"=>"Discourse/3.5.0.beta2-dev-a6066732c67b1430655d1bdfe3c54ee271bc1569; +https://www.discourse.org/",
   "Accept"=>"*/*",
   "Accept-Encoding"=>"gzip;q=1.0,deflate;q=0.6,identity;q=0.3"},
 "parsedQueryParams"=>{}}

@pento pento self-assigned this Feb 28, 2025
Copy link
Contributor

@martin-brennan martin-brennan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving as is, comment is food for thought / future refactors.

@martin-brennan
Copy link
Contributor

@pento found another place where we are hardcoding another user-agent format, for twitter oneboxes:

def http_params
{ "User-Agent" => "DiscourseBot/1.0" }
end

@pento pento merged commit 8615fc6 into main Mar 3, 2025
17 checks passed
@pento pento deleted the pento/dev-add-user-agent-to-all-http-requests branch March 3, 2025 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants