Dean

Blocking API access for requests without the User-Agent header

While working on the Desktoply v3 API, looking around at other APIs I found that Basecamp require the use of the User-Agent header and if this header isn’t set, then a 400 Bad Request error will be returned.

This looked like a good idea so I got to work on adding it to the Desktoply API I’m working on for v3.

# In api/base_controller.rb
before_filter :check_user_agent_header

private

def check_user_agent_header
  user_agent = request.headers['User-Agent']

  if user_agent.nil?
    render :status => 400, :json => { :error => "The User-Agent header isn't present. Please see http://help.desktop.ly/kb/api-documentation/introduction" }
    return
  end
end

Nice and simple!

Copyright 2011-2021 Dean (deanpcmad)

Buy me a Coffee