title: Sinatra Content Type both json and form
body: <p>I have been looking all over for how to properly check respond to a application/json type as well as a submitted form. I finally got it working on my own with the following code. Can someone explain why it works? Or offer advice on a better solution to achieve the same thing?</p>
  
  <pre><code>post '/login', provides: :json do
  p = params
  if request.content_type == 'application/json'
    params = JSON.parse(request.body.read, :symbolize_names =&gt; true)
  else 
    params = p
  end
  
  
  requires(params, :email, :password)
  
  if @user = User.find_by_email(params[:email])
    if @user.authenticate(params[:password])
      log_user_in(@user)
      rabl :login, object: @user
    else
      error 404, {error: "incorrect credentials"}.to_json
    end
  else
    error 404, {error: "user not found"}.to_json
  end
  </code></pre>
  
  <p>end</p>