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 4623914
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:03:22+00:00 2026-05-22T03:03:22+00:00

I have a JSON and XML based API that needs to be page cached.

  • 0

I have a JSON and XML based API that needs to be page cached. I have setup my routes on the api to include the format as part of the URL, such that URL’s like this work:

http://example.com/foo/1/bar/2/xml
http://example.com/foo/1/bar/2/json

The problem I am seeing is that in the server’s public folder, the files are being saved as xml.xml and json.json, and this causes cache misses the next time the URL is accessed.

Is there a way to either:

  1. Turn off the auto extension generation so that they are saved without an extension at all? (EX: RAILS_ROOT/public/foo/1/bar/2/json)
  2. Force all the extensions to be .html for every call. (EX: RAILS_ROOT/public/foo/1/bar/2/json.html)

Either of these would cause my server to return the cached file instead of a miss. How can I do this?

EDIT:
Somebody asked for the relevant route:

scope '(foo/:foo_id)', :foo_id => /\d+/ do
  get '/bar/:bar_id/:format' => 'bars#show', :bar_id => /\d+/, :format => /json|xml|html/
end

SOLUTION:
While I was looking for an official way to make this happen using the built in page caching support, I ended up just using an after filter and my own page cache method, as suggested by Anton

# application_controller.rb
def cache_api_page
  if REDACTEDServer::Application.config.action_controller.perform_caching
    self.class.cache_page(response.body, request.path, '')
    puts "CACHED PATH: #{request.path}"
  end
end

# bar_controller.rb
 after_filter :cache_api_page, :only => [ :show, :index ]
  • 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-05-22T03:03:23+00:00Added an answer on May 22, 2026 at 3:03 am

    You can do that like this:

    class FooController < ApplicationController
    
      after_filter(:only => :show, :if => Proc.new { |c| c.request.format.json? }) do |controller|
        controller.class.cache_page(controller.response.body, controller.request.path, '.html')
      end
    
    end
    

    When http://example.com/foo/1/bar/2/json is accessed, it will write page to cache (RAILS_ROOT/public/foo/1/bar/2/json.html)

    And if you get http://example.com/foo/1/bar/2/json again, you receive RAILS_ROOT/public/foo/1/bar/2/json.html, but your http server(Apache?) should know about content type of this files.

    Otherwise content type will set to ‘text/html’

    UPDATE

    To you .htaccess

    <FilesMatch "\/json$">
    <IfModule mod_headers.c>
      Header set Content-Type "text/json"
    </IfModule>
    </FilesMatch>
    
    
    <FilesMatch "\/xml$">
    <IfModule mod_headers.c>
      Header set Content-Type "text/xml"
    </IfModule>
    </FilesMatch>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one JSON that is coming in a string format. I need to
I basically have the following flow: XML -> JSON -> Spring MVC -> jsp
I have a JSON result that contains numerous records. I'd like to show the
Hi I have a JSON object that is a 2-dimentional array and I need
I have a dilemna that needs I've been thinking a while but still haven't
I have a new web app that is packaged as a WAR as part
I have an XML web service that javascript calls are made using jQuery. This
I have a Java WebService setup which consumes an xml file and want to
I am creating an XML based application in which I have to fetch data
I have a json object that is actually an arraylist of objects previously converted

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.