---
title: "rails中如何添加自定义的请求格式"
author: "RunUpwind"
date: "2011-06-22T11:28:18Z"
tags: []
categories: ["编程语言"]
slug: "rails"
draft: false
lastmod: "2011-06-22T11:28:18Z"
id: 1100695
url: "https://runupwind.iteye.com/blog/1100695"
---

rails默认request的格式是 text/html.  同时还支持 xml 和 json.

 

如果要添加一个格式如.api,  访问路径：http://..../login.api,   该如何做？

 

参照：http://stackoverflow.com/questions/2456219/add-a-custom-format-in-rails-that-will-work-with-respond-to

```
# add in Environment.rb
Mime::Type.register "api/json", :api
```

 

在controller中，可以用request.format 取得值为 'api/json'