Home > Enterprise >  400 Bad Request for Ruby RSS gem
400 Bad Request for Ruby RSS gem

Time:02-04

I can't seem to get this RSS feed to work properly. I've tried Nokogiri and now RSS::Parser and neither work:

a = 'https://phys.org/rss-feed/biology-news/biology-other/'
URI.open(a) do |rss|
  feed = RSS::Parser.parse(rss)
  puts "Title: #{feed.channel.title}"
  feed.items.each do |item|
    puts "Item: #{item.title}"
  end
end

The code is taken directly out of the docs: https://github.com/ruby/rss

The feed is valid, so I'm confused as to why there's a 400 error code.

What am I doing wrong? Anybody have insight as to how to get this RSS parsed?

Here is the error:

/Users/user3/.rbenv/versions/3.1.2/lib/ruby/3.1.0/open-uri.rb:364:in `open_http': 400 Bad request (OpenURI::HTTPError)
    from /Users/user3/.rbenv/versions/3.1.2/lib/ruby/3.1.0/open-uri.rb:741:in `buffer_open'
    from /Users/user3/.rbenv/versions/3.1.2/lib/ruby/3.1.0/open-uri.rb:212:in `block in open_loop'
    from /Users/user3/.rbenv/versions/3.1.2/lib/ruby/3.1.0/open-uri.rb:210:in `catch'
    from /Users/user3/.rbenv/versions/3.1.2/lib/ruby/3.1.0/open-uri.rb:210:in `open_loop'
    from /Users/user3/.rbenv/versions/3.1.2/lib/ruby/3.1.0/open-uri.rb:151:in `open_uri'
    from /Users/user3/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/open_uri_redirections-0.2.1/lib/open-uri/redirections_patch.rb:55:in `open_uri'
    from /Users/user3/.rbenv/versions/3.1.2/lib/ruby/3.1.0/open-uri.rb:721:in `open'
    from /Users/user3/.rbenv/versions/3.1.2/lib/ruby/3.1.0/open-uri.rb:29:in `open'
    from /users/user3/app.rb:1856:in `<main>'

CodePudding user response:

The web server requires the request to have a User-Agent set in the headers. Without such a User-Agent header it returns the 400 error message.

require 'uri'
require 'open-uri'
require 'rss'

uri = URI.parse("https://phys.org/rss-feed/biology-news/biology-other/")
uri.open("User-Agent" => "Ruby/#{RUBY_VERSION}") do |rss|
  feed = RSS::Parser.parse(rss)

  puts "Title: #{feed.channel.title}"
  feed.items.each do |item|
    puts "Item: #{item.title}"
  end
end

This code work for me.

  • Related