[{"data":1,"prerenderedAt":624},["ShallowReactive",2],{"Categories":3,"NavIndexCategoriesCountFooter":203,"content-\u002F2010\u002F03\u002F18\u002Fruby-on-rails-and-ie-8-respond-to-and-http-accept-headers\u002F":204},[4,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,68,70,71,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202],{"category":5},"System Administration",{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":27},"Software Development",{"category":5},{"category":5},{"category":5},{"category":5},{"category":27},{"category":27},{"category":5},{"category":5},{"category":5},{"category":27},{"category":5},{"category":5},{"category":5},{"category":27},{"category":27},{"category":27},{"category":27},{"category":5},{"category":5},{"category":5},{"category":27},{"category":27},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":5},{"category":27},{"category":5},{"category":5},{"category":27},{"category":27},{"category":27},{"category":27},{"category":5},{"category":27},{"category":27},{"category":67},"Drones & RC",{"category":69},"DIY Projects",{"category":67},{"category":72},"Photography",{"category":69},{"category":69},{"category":69},{"category":67},{"category":69},{"category":69},{"category":69},{"category":69},{"category":69},{"category":69},{"category":69},{"category":69},{"category":69},{"category":69},{"category":69},{"category":69},{"category":69},{"category":69},{"category":67},{"category":69},{"category":69},{"category":67},{"category":67},{"category":72},{"category":72},{"category":72},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":5},{"category":5},{"category":72},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":5},{"category":67},{"category":67},{"category":72},{"category":72},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":67},{"category":72},{"category":67},{"category":138},"3D Printing - Laser Cutting - CNC",{"category":138},{"category":138},{"category":138},{"category":138},{"category":138},{"category":138},{"category":138},{"category":138},{"category":138},{"category":138},{"category":138},{"category":5},{"category":138},{"category":27},{"category":27},{"category":138},{"category":138},{"category":72},{"category":158},"Photography,3D Printing - Laser Cutting - CNC",{"category":27},{"category":27},{"category":69},{"category":27},{"category":27},{"category":27},{"category":27},{"category":5},{"category":67},{"category":5},{"category":5},{"category":27},{"category":27},{"category":27},{"category":27},{"category":27},{"category":69},{"category":27},{"category":27},{"category":27},{"category":27},{"category":181},"Home Assistant",{"category":181},{"category":72},{"category":27},{"category":27},{"category":72},{"category":138},{"category":5},{"category":72},{"category":72},{"category":138},{"category":27},{"category":181},{"category":181},{"category":72},{"category":72},{"category":72},{"category":72},{"category":72},{"category":72},{"category":72},{"category":72},191,{"id":205,"title":206,"body":207,"category":27,"date":609,"description":213,"embedImage":610,"extension":611,"image":610,"intro":610,"meta":612,"navigation":438,"path":613,"seo":614,"series":610,"sitemap":615,"stem":616,"tags":617,"__hash__":623},"content\u002F2010\u002F03\u002F18\u002Fruby-on-rails-and-ie-8-respond-to-and-http-accept-headers.md","Ruby on Rails and IE 8 - respond_to and HTTP accept headers",{"type":208,"value":209,"toc":605},"minimark",[210,214,232,240,243,248,251,291,294,363,366,509,512,516,519,522,525,595,598,601],[211,212,213],"p",{},"Ruby on Rails offers the ability to respond to a given request with different types depending on the URL.",[211,215,216,217,226,227,231],{},"For example - ",[218,219,220],"a",{"href":220,"rel":221,"target":225},"http:\u002F\u002Fserver\u002Fcontroller.html",[222,223,224],"nofollow","noopener","noreferer","_blank"," gives HTML where ",[218,228,229],{"href":229,"rel":230,"target":225},"http:\u002F\u002Fserver\u002Fcontroller.pdf",[222,223,224]," sends a PDF file.",[211,233,234,235,239],{},"However - what happens if the URL is just ",[218,236,237],{"href":237,"rel":238,"target":225},"http:\u002F\u002Fserver\u002Fcontroller",[222,223,224]," ? Well then rails will use the HTTP_ACCEPT header sent by the browser (at least in 2.3.x - haven't tested other versions).",[211,241,242],{},"So - when adding support for \u002Fcontroller.xls it seemed a little strange that all users apart from IE got the HTML page where IE8 (I suspect more but have only tested with v8) got the excel file.",[244,245,247],"h2",{"id":246},"the-problem","The problem",[211,249,250],{},"The issue is in the accept header sent by IE:",[252,253,254,258,261,264,267,270,273,276,279,282,285,288],"ul",{},[255,256,257],"li",{},"image\u002Fgif",[255,259,260],{},"image\u002Fjpeg",[255,262,263],{},"image\u002Fpjpeg",[255,265,266],{},"application\u002Fx-ms-application",[255,268,269],{},"application\u002Fvnd.ms-xpsdocument",[255,271,272],{},"application\u002Fxaml+xml",[255,274,275],{},"application\u002Fx-ms-xbap",[255,277,278],{},"application\u002Fx-shockwave-flash",[255,280,281],{},"application\u002Fvnd.ms-excel",[255,283,284],{},"application\u002Fvnd.ms-powerpoint",[255,286,287],{},"application\u002Fmsword",[255,289,290],{},"*\u002F*",[211,292,293],{},"If we change",[295,296,301],"pre",{"className":297,"code":298,"language":299,"meta":300,"style":300},"language-ruby shiki shiki-themes github-dark","    respond_to do |format|\n      format.html # index.html.erb\n      format.xml  { render :xml => @items }\n    end\n","ruby","",[302,303,304,320,338,357],"code",{"__ignoreMap":300},[305,306,309,313,317],"span",{"class":307,"line":308},"line",1,[305,310,312],{"class":311},"s95oV","    respond_to ",[305,314,316],{"class":315},"snl16","do",[305,318,319],{"class":311}," |format|\n",[305,321,323,327,330,334],{"class":307,"line":322},2,[305,324,326],{"class":325},"sDLfK","      format",[305,328,329],{"class":311},".",[305,331,333],{"class":332},"svObZ","html",[305,335,337],{"class":336},"sAwPA"," # index.html.erb\n",[305,339,341,343,345,348,351,354],{"class":307,"line":340},3,[305,342,326],{"class":325},[305,344,329],{"class":311},[305,346,347],{"class":332},"xml",[305,349,350],{"class":311},"  { render ",[305,352,353],{"class":325},":xml",[305,355,356],{"class":311}," => @items }\n",[305,358,360],{"class":307,"line":359},4,[305,361,362],{"class":315},"    end\n",[211,364,365],{},"to",[295,367,369],{"className":297,"code":368,"language":299,"meta":300,"style":300},"    respond_to do |format|\n      format.html # index.html.erb\n      format.xml  { render :xml => @items }\n      format.xls do\n        excel = Item.get_excel\n\n        send_file(excel.get_spreadsheet_tempfile_name,\n                  :type => 'application\u002Fvnd.ms-excel',\n                  :disposition => 'attachment',\n                  :filename => excel.get_destination_filename)\n      end\n    end\n",[302,370,371,379,389,403,415,433,440,455,470,483,498,504],{"__ignoreMap":300},[305,372,373,375,377],{"class":307,"line":308},[305,374,312],{"class":311},[305,376,316],{"class":315},[305,378,319],{"class":311},[305,380,381,383,385,387],{"class":307,"line":322},[305,382,326],{"class":325},[305,384,329],{"class":311},[305,386,333],{"class":332},[305,388,337],{"class":336},[305,390,391,393,395,397,399,401],{"class":307,"line":340},[305,392,326],{"class":325},[305,394,329],{"class":311},[305,396,347],{"class":332},[305,398,350],{"class":311},[305,400,353],{"class":325},[305,402,356],{"class":311},[305,404,405,407,409,412],{"class":307,"line":359},[305,406,326],{"class":325},[305,408,329],{"class":311},[305,410,411],{"class":332},"xls",[305,413,414],{"class":315}," do\n",[305,416,418,422,425,428,430],{"class":307,"line":417},5,[305,419,421],{"class":420},"s9osk","        excel",[305,423,424],{"class":315}," =",[305,426,427],{"class":325}," Item",[305,429,329],{"class":311},[305,431,432],{"class":332},"get_excel\n",[305,434,436],{"class":307,"line":435},6,[305,437,439],{"emptyLinePlaceholder":438},true,"\n",[305,441,443,446,449,452],{"class":307,"line":442},7,[305,444,445],{"class":332},"        send_file",[305,447,448],{"class":311},"(excel.",[305,450,451],{"class":332},"get_spreadsheet_tempfile_name",[305,453,454],{"class":311},",\n",[305,456,458,461,464,468],{"class":307,"line":457},8,[305,459,460],{"class":325},"                  :type",[305,462,463],{"class":311}," => ",[305,465,467],{"class":466},"sU2Wk","'application\u002Fvnd.ms-excel'",[305,469,454],{"class":311},[305,471,473,476,478,481],{"class":307,"line":472},9,[305,474,475],{"class":325},"                  :disposition",[305,477,463],{"class":311},[305,479,480],{"class":466},"'attachment'",[305,482,454],{"class":311},[305,484,486,489,492,495],{"class":307,"line":485},10,[305,487,488],{"class":325},"                  :filename",[305,490,491],{"class":311}," => excel.",[305,493,494],{"class":332},"get_destination_filename",[305,496,497],{"class":311},")\n",[305,499,501],{"class":307,"line":500},11,[305,502,503],{"class":315},"      end\n",[305,505,507],{"class":307,"line":506},12,[305,508,362],{"class":315},[211,510,511],{},"then everyone will get the excel file if they request \u002Fcontroller.xls but IE gets the excel file also for \u002Fcontroller - instead of the first entry in the list - the HTML response. From the accept headers list you can see that the excel mimetype is specifically listed.",[244,513,515],{"id":514},"the-workaround","The Workaround",[211,517,518],{},"Most googling suggested setting the order of the respond_to elements - but here the HTML response is first.",[211,520,521],{},"So - as a workaround - force prefix the accept header before the call to respond_to",[211,523,524],{},"For this example I just grabbed the HTML part of the Safari 4 accept header and prefixed it.",[295,526,528],{"className":297,"code":527,"language":299,"meta":300,"style":300},"  def set_accept_header\n    accept = request.env[\"HTTP_ACCEPT\"]\n\n    request.env[\"HTTP_ACCEPT\"] = \"application\u002Fxml,application\u002Fxhtml+xml,text\u002Fhtml;q=0.9,#{accept}\"\n  end\n",[302,529,530,538,560,564,590],{"__ignoreMap":300},[305,531,532,535],{"class":307,"line":308},[305,533,534],{"class":315},"  def",[305,536,537],{"class":332}," set_accept_header\n",[305,539,540,543,545,548,551,554,557],{"class":307,"line":322},[305,541,542],{"class":420},"    accept",[305,544,424],{"class":315},[305,546,547],{"class":311}," request.",[305,549,550],{"class":332},"env",[305,552,553],{"class":311},"[",[305,555,556],{"class":466},"\"HTTP_ACCEPT\"",[305,558,559],{"class":311},"]\n",[305,561,562],{"class":307,"line":340},[305,563,439],{"emptyLinePlaceholder":438},[305,565,566,569,571,573,575,578,581,584,587],{"class":307,"line":359},[305,567,568],{"class":311},"    request.",[305,570,550],{"class":332},[305,572,553],{"class":311},[305,574,556],{"class":466},[305,576,577],{"class":311},"] ",[305,579,580],{"class":315},"=",[305,582,583],{"class":466}," \"application\u002Fxml,application\u002Fxhtml+xml,text\u002Fhtml;q=0.9,",[305,585,586],{"class":466},"#{accept}",[305,588,589],{"class":466},"\"\n",[305,591,592],{"class":307,"line":417},[305,593,594],{"class":315},"  end\n",[211,596,597],{},"This can be called as a standard method call in the directly affected method or as a before_filter - depending on your needs.",[211,599,600],{},"Note that this is a very simplified method - it doesn't check to see what's present there or if it is IE that is the client - but for the application it was needed for its good enough ;)",[602,603,604],"style",{},"html pre.shiki code .s95oV, html code.shiki .s95oV{--shiki-default:#E1E4E8}html pre.shiki code .snl16, html code.shiki .snl16{--shiki-default:#F97583}html pre.shiki code .sDLfK, html code.shiki .sDLfK{--shiki-default:#79B8FF}html pre.shiki code .svObZ, html code.shiki .svObZ{--shiki-default:#B392F0}html pre.shiki code .sAwPA, html code.shiki .sAwPA{--shiki-default:#6A737D}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .s9osk, html code.shiki .s9osk{--shiki-default:#FFAB70}html pre.shiki code .sU2Wk, html code.shiki .sU2Wk{--shiki-default:#9ECBFF}",{"title":300,"searchDepth":322,"depth":322,"links":606},[607,608],{"id":246,"depth":322,"text":247},{"id":514,"depth":322,"text":515},"2010-03-18 23:35:15 +0100",null,"md",{},"\u002F2010\u002F03\u002F18\u002Fruby-on-rails-and-ie-8-respond-to-and-http-accept-headers",{"title":206,"description":213},{"loc":613},"2010\u002F03\u002F18\u002Fruby-on-rails-and-ie-8-respond-to-and-http-accept-headers",[618,619,620,621,622],"rails","ruby on rails","respond-to","http-accept","ie8","tcSmTE5QLilSPmecOLUEr5uCAWA_mkQa9d8Uhe3G2Hs",1775293014459]