Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8672529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:13:43+00:00 2026-06-12T19:13:43+00:00

I’m creating a Sinatra web application which I would like to serve mime type

  • 0

I’m creating a Sinatra web application which I would like to serve mime type application/xhtml+xml for all web pages served. I am using Builder as template engine. Note that the application will also be serving application/xml for various AJAX api interfaces, also using the Builder template engine, as well as text/css using the scss template engine.

I notice that the webpages that get generated receive a text/html content type while the api interfaces get an application/xml content type instead. However, I don’t know how the mime type is decided; presence of the html tag? usage of a layout or not? Whichever the case, I would like the application to issue a application/xhtml+xml mimetype instead of the text/html mime type.

I know I can specify the mime type with the content_type call, but I’d like to refrain from needing this in every routing function. Is there any way I can set a default mime type for a given template engine? Or can the mime type be controlled by the view’s filename? Does Tilt provide any means to control mime type in a nice way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:13:44+00:00Added an answer on June 12, 2026 at 7:13 pm

    Since your application is factored into separate objects, it should be straightforward to pull all of the AJAX routes into a separate Sinatra application. This will allow you to use Sinatra’s after hook. The only requirement is for your AJAX calls to have something that identifies them as AJAX; for example, by setting a request header to a special value, or by using a specially formatted route. Here is an example rackup file of the unique route method:

    require 'sinatra/base'
    
    class MainApp
    
      def get_info params
        # return some data structure
      end
    
    end
    
    class MainAppRoutes < Sinatra::Base
    
      def initialize mainapp
        super()
        @mainapp = mainapp
      end
    
      get '/' do # main page
        data = @mainapp.get_info(params)
        # render response from data
      end
    
    end
    
    class AjaxRoutes < Sinatra::Base
    
      def initialize mainapp
        super()
        @mainapp = mainapp
      end
    
      get '/getinfo' do # handler for /ajax/getinfo
        data = @mainapp.get_info(params)
        # generate XML response from data
      end
    
      after do
        content_type 'application/xml'
      end
    
    end
    
    mainapp = MainApp.new
    
    map '/ajax' do
      run AjaxRoutes.new(mainapp)
    end
    run MainAppRoutes.new(mainapp)
    

    In this example, all routes starting with /ajax are handled by instances of the AjaxRoutes class. The after hook ensures that the content type for those responses are ‘application/xml’. The non-ajax routes are not affected.

    A few things to remember:

    • Rack::Builder.map strips off the parts of the URL it matches. So a ‘/’ route in AjaxRoutes would actually answer a request for ‘/ajax/’.
    • Always call super() in the initializers of your Sinatra-derived endpoint classes. For Sinatra middleware, pass the first argument up (Rack calls #new with the next rack app as the first parameter).
    • Review the doc about Sinatra filters. There is a potentially frustrating quirk about modifying responses with the after hook.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I would like to count the length of a string with PHP. The string
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.