本文介绍了了解http请求格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我从Google Chrome浏览器中提取的3个http标头请求格式。这3种请求格式适用于html,xml和json。服务器如何知道哪个是html,xml或json并做出相应的响应?
Following are 3 http header request format i have extracted from google chrome browser. Those 3 request format are for html, xml and json. How the server will know which one is html, xml or json and respond accordingly?
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:_depot_session=BAh7CUkiD3Nlc3Npb25faWQGOgZFRiIlZjhlMzg2MjM3YWJlYzg2NzgyZTk0MmM2MzQzOTI1YWVJIgxjYXJ0X2lkBjsARmksSSIQX2NzcmZfdG9rZW4GOwBGSSIxMk9PaHhzVVpqNkJ2VnYxQnZwL3E5UWlZbFFjKzF3Mm9CUEhtT2c2TWd0VT0GOwBGSSIMdXNlcl9pZAY7AEZpDA%3D%3D--e672d35300801a62050521d5d2ed381f263126a7
Host:localhost:3000
Referer:http://localhost:3000/en/products
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:69
Content-Type:application/x-www-form-urlencoded
Cookie:_depot_session=BAh7CUkiD3Nlc3Npb25faWQGOgZFRiIlZjhlMzg2MjM3YWJlYzg2NzgyZTk0MmM2MzQzOTI1YWVJIgxjYXJ0X2lkBjsARmksSSIQX2NzcmZfdG9rZW4GOwBGSSIxMk9PaHhzVVpqNkJ2VnYxQnZwL3E5UWlZbFFjKzF3Mm9CUEhtT2c2TWd0VT0GOwBGSSIMdXNlcl9pZAY7AEZpDA%3D%3D--e672d35300801a62050521d5d2ed381f263126a7
Host:localhost:3000
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:69
Content-Type:application/x-www-form-urlencoded
Cookie:_depot_session=BAh7CUkiD3Nlc3Npb25faWQGOgZFRiIlZjhlMzg2MjM3YWJlYzg2NzgyZTk0MmM2MzQzOTI1YWVJIgxjYXJ0X2lkBjsARmksSSIQX2NzcmZfdG9rZW4GOwBGSSIxMk9PaHhzVVpqNkJ2VnYxQnZwL3E5UWlZbFFjKzF3Mm9CUEhtT2c2TWd0VT0GOwBGSSIMdXNlcl9pZAY7AEZpDA%3D%3D--e672d35300801a62050521d5d2ed381f263126a7
Host:localhost:3000
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
推荐答案
您有一个名为将负责处理请求
You have a method called respond_to which will take care of the requests
def index
@people = Person.find(:all)
respond_to do |format|
format.html
format.xml { render :xml => @people.to_xml }
format.json {render :json => @people.to_json}
end
end
控制器中的上述方法将根据请求格式回复
The above method in controller will respond according to the request format
这篇关于了解http请求格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!