<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Tewha.net</title>
    <link>https://tewha.net/tags/nsurlrequest/</link>
    <description>Recent content on Tewha.net</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Wed, 30 May 2012 14:00:00 +0000</lastBuildDate>
    <atom:link href="https://tewha.net/tags/nsurlrequest/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Handling 302/303 redirects</title>
      <link>https://tewha.net/2012/05/handling-302303-redirects/</link>
      <pubDate>Wed, 30 May 2012 14:00:00 +0000</pubDate>
      <guid>https://tewha.net/2012/05/handling-302303-redirects/</guid>
      <description>&lt;p&gt;If you try to POST to a web API using &lt;code&gt;NSURLConnection&lt;/code&gt; that redirects you using a &lt;a href=&#34;http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3&#34;&gt;302&lt;/a&gt; or &lt;a href=&#34;http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4&#34;&gt;303&lt;/a&gt; redirect, you’ll fall over to a GET request. This is intentional, but you can override it.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>If you try to POST to a web API using <code>NSURLConnection</code> that redirects you using a <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.3">302</a> or <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4">303</a> redirect, you’ll fall over to a GET request. This is intentional, but you can override it.</p>
<p>With HTTP status codes 302 and 303, the user agent (in this case, <code>NSURLConnection</code>) will change the request type from POST to GET. This is just how 302 was usually implemented; status code 303 was added to HTTP/1.1 to make this behaviour explicit.</p>
<p>HTTP/1.1 also added a status code to redirect without changing the request type from POST to GET. If you can change the web site, getting it to redirect you with a <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8">307</a> instead will fix the problem.</p>
<p>If not, you can make <code>NSURLRequest</code> ignore this aspect of RFC2616.</p>
<p><code>NSURLRequest</code> sends an event to its delegate, in which you can see what <code>NSURLRequest</code> plans to do and customize it:</p>
<pre><code>- (NSURLRequest *)connection: (NSURLConnection *)connection
             willSendRequest: (NSURLRequest *)request
            redirectResponse: (NSURLResponse *)redirectResponse;
{
    if (redirectResponse) {
        NSMutableURLRequest *r = [[originalRequest mutableCopy] autorelease];
        [r setURL: [request URL]];
        return r;
    } else {
        return request;
    }
}
</code></pre>
<p>By doing this, you’re cloning the original request and changing the URL to match the request suggested by <code>NSURLConnection</code> (which is following the RFC).</p>
<p>See also:</p>
<ul>
<li>w3.org: <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">RFC2616 HTTP/1.1, section 10: Status Code Definitions</a></li>
</ul>]]></content:encoded>
    </item>
  </channel>
</rss>
