<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Floorplanner Tech Blog &#187; request</title>
	<atom:link href="http://techblog.floorplanner.com/tag/request/feed/" rel="self" type="application/rss+xml" />
	<link>http://techblog.floorplanner.com</link>
	<description>Our latest geek adventures!</description>
	<lastBuildDate>Tue, 16 Mar 2010 18:45:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating a REST API for a Flash application</title>
		<link>http://techblog.floorplanner.com/2009/02/25/creating-a-rest-api-for-a-flash-application/</link>
		<comments>http://techblog.floorplanner.com/2009/02/25/creating-a-rest-api-for-a-flash-application/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 16:49:47 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Flash+ActionScript]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[caveats]]></category>
		<category><![CDATA[DELETE]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[PUT]]></category>
		<category><![CDATA[request]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=486</guid>
		<description><![CDATA[We have been working hard to implement an XML-based REST-style API for Floorplanner and some of our partners are using it already to access their users and plans. We now have started to use this API ourselves.
The Floorplanner Flash application communicates with our servers to load and save projects and designs. The backend for this [...]]]></description>
			<content:encoded><![CDATA[<p>We have been working hard to implement an XML-based REST-style API for Floorplanner and some of our partners are using it already to access their users and plans. We now have started to use this API ourselves.</p>
<p>The Floorplanner Flash application communicates with our servers to load and save projects and designs. The backend for this functionality used to be written in PHP. Eliminating this PHP application simplifies our server setup, eases development and reduces our maintenance burden. To rewrite this functionality in Rails, we decided to eat our own dogfood and use the REST API to load and save designs. Why reinvent the wheel?</p>
<p>However, while implementing the changes it the Flash application, we found that it did not work out of the box, because of some limitations in ActionScript. Note that we still use ActionScript 2, so some of these issues may not be relevant for ActionScript 3. In this post, we will detail what these issues are and how we overcame them.</p>
<h3> Using a separate format </h3>
<p>Our REST API uses the XML format supplied by Rails. Because we had to make some changes to make it work from ActionScript, we decided to add a distinct format that we could implement differently without altering the behavior of the default XML API. Adding a new format, called &#8220;flash&#8221; can be done in Rails by adding a MIME alias to your environment:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#6666ff; font-weight:bold;">Mime::Type</span>.<span style="color:#9900CC;">register_alias</span> <span style="color:#996600;">&quot;application/xml&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:flash</span></pre></div></div>

<p>Now we can send different responses based on this format:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>   <span style="color:#006600; font-weight:bold;">&#123;</span> ... <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;"># default API behavior</span>
  <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">flash</span> <span style="color:#006600; font-weight:bold;">&#123;</span> ... <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;"># do something different</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3> HTTP status codes </h3>
<p>REST APIs use HTTP status codes to return whether a call succeeded, and if not, why. Flash however uses the browser to perform HTTP requests. The browser only returns something to Flash if the request was successful. So, if an error code is used if a request failed together with some error messages, these error messages will not be delivered to Flash and cannot be displayed to the user. We resolved this by always sending the HTTP OK status.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>   <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@project</span>.<span style="color:#9900CC;">errors</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">422</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">flash</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> XML.<span style="color:#9900CC;">failure</span><span style="color:#006600; font-weight:bold;">&#40;</span>@project.<span style="color:#9900CC;">errors</span>, <span style="color:#006666;">422</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Our <code>XML.failure</code> method will return something like:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family: Monaco,monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;failure</span> <span style="color: #000066;">status</span>=<span style="color: #ff0000;">&quot;422&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;error</span> <span style="color: #000066;">on</span>=<span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>A project should have a name!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/error<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/failure<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Note that other HTTP success statuses than 200 work in Safari and Firefox, but not in Internet Explorer. So, never return a 201 (<code>:created</code>), because Internet Explorer will not send the result to Flash!</p>
<h3> PUT and DELETE requests </h3>
<p>REST-style APIs use HTTP PUT requests to alter objects and DELETE requests to destroy objects. Most browsers do not support these request type. It is not supported by ActionScript either, because Flash uses the browser to send the request. </p>
<p>To overcome this problem, these types of requests can be simulated in Rails by sending a <code>_method</code> parameter along with a POST request. Unfortunately, this does not work when calling the REST API. The POST request body cannot be used to send additional variables, because it is used for the XML payload.</p>
<p>We solved this issue by creating additional routes for POST requests to the <code>update</code> and <code>destroy</code> actions of our resource controllers. Our <code>routes.rb</code> file now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">  map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:projects</span>, <span style="color:#ff3333; font-weight:bold;">:member</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:update</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:post</span>, <span style="color:#ff3333; font-weight:bold;">:destroy</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:post</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>These routes route to exactly the same methods as the default REST actions (<code>ProjectsController#update</code> and <code>ProjectsController#destroy</code>), so no additional code is needed. The following calls are now equivalent:</p>
<pre>
PUT    /projects/123.xml
POST   /projects/123/update.xml
POST   /projects/123/update.flash

DELETE /projects/123.xml
POST   /projects/123/destroy.xml
POST   /projects/123/destroy.flash
</pre>
<h3>The result </h3>
<p>It requires some stretching of the pure REST principles, but doing so is worth it: we can now reuse the code we use for our API to handle Flash application calls and we can eliminate the PHP backend.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2009/02/25/creating-a-rest-api-for-a-flash-application/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Crossdomain JSON troubles: JSONCrossdomainRequest</title>
		<link>http://techblog.floorplanner.com/2008/12/05/crossdomain-ajax-json-request-troubles/</link>
		<comments>http://techblog.floorplanner.com/2008/12/05/crossdomain-ajax-json-request-troubles/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 12:58:07 +0000</pubDate>
		<dc:creator>jaap</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[crossdomain]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[request]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=292</guid>
		<description><![CDATA[This week I was doing some work on a crossdomain JSON request. This was a pitty, because I had in mind, Ajax requests could be made between subdomains, but that wasn&#8217;t the case, therefore I had to come up with a solution.
Solution 1: Server-side proxy
One option we had, was a server-side proxy, but I didn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>This week I was doing some work on a crossdomain JSON request. This was a pitty, because I had in mind, Ajax requests could be made between subdomains, but that wasn&#8217;t the case, therefore I had to come up with a solution.</p>
<p><strong>Solution 1: Server-side proxy</strong><br />
One option we had, was a server-side proxy, but I didn&#8217;t like this solution. If you are proxying your crossdomain request through a webserver that has to handle a lot of requests per second, these requests can block other requests, because proxying is always a slow process.</p>
<p><strong>Solution 2: JSONP</strong><br />
Then there is the <a href="http://Ajaxian.com/archives/jsonp-json-with-padding">JSONP method</a>, which includes a script on the fly in the browser and this script is then evaluated. The problem however with this approach is that your JSON resources have to be rewritten as a evaluation. This evaluation is then executed and your JSON loaded in a variable.</p>
<p>Instead of:</p>
<pre><code>{"somejsonobject":1}
</code></pre>
<p>you have to write:</p>
<pre><code>var jsonobject = {"somejsonobject":1};
someCallback(jsonobject);
</code></pre>
<p>This is not very clean, because you have to decide in your remote JSON file what the variable will be named. JSONP is a method in which you give a variable and callback in the url and then the JSON will be rewritten. This solution is just not the right way, as we programmers all know why it is better to isolate logic from real data.</p>
<p><strong>Solution 3: Crossdomain through flash</strong><br />
Then I looked at another solution that does crossdomain Ajax requests through a flash proxy. These solutions all gave a lot of problems, cause the encoding of characters was not handled properly by the Flash ExternalInterface compononent, which sended the JSON string it received to Javascript.</p>
<p><strong>Solution 4: Parsing the JSON by flash</strong><br />
I then tried to make a solution myself that doesn&#8217;t send JSON string to Javascript, but (thanks for the idea <a href="http://www.twitter.com/hertog">Gert-Jan</a>) sends the objects to Javascript. So the JSON is parsed into an object at the Actionscript side and it now all works. The project is opensourced under a MIT license and called: JSONCrossdomainRequest. It is a js file which includes a swf file in your page that can handle the requests. Please come over to GitHub and read more about how to install and use this project. The Actionscript project is also included. Good luck if you are going to use it and if you have any modifications let me know, it is a work in progress and we appreciate any help!</p>
<p><strong>Project location:</strong><br />
<a href="http://github.com/japetheape/jsoncrossdomainrequest/tree/master">http://github.com/japetheape/jsoncrossdomainrequest/tree/master</a></p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2008/12/05/crossdomain-ajax-json-request-troubles/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
