Skip to main content
Ben Nadel at dev.Objective() 2015 (Bloomington, MN) with: Jessica Kennedy
Ben Nadel at dev.Objective() 2015 (Bloomington, MN) with: Jessica Kennedy

Fetch API Will Propagate Non-POST Methods Upon Redirect

By
Published in , Comments (6)

In the book Hypermedia Systems by Carson Gross, the authors mention that the browser will propagate the HTTP Method upon redirect. In all my years of programming, I had never seen this; so, I sanity checked it and put the issue to rest in my head. Then, yesterday, I was listening to the Remote Ruby podcast with Chris Oliver and Andrew Mason, and they too mentioned this HTTP method propagation specifically for the JavaScript fetch() API. I feel like I was taking crazy-pills; so, I went to sanity check it again; and, discovered that the fetch() API will propagate the HTTP method; but, only for non-GET/POST methods.

To test this, I created two ColdFusion end-points:

  • api.cfm - this does nothing but perform a 302 redirect to api-2.cfm.

  • api-2.cfm - this does nothing but echo cgi.http_method.

Then, I created a simple ColdFusion page that has buttons for testing each HTTP method using fetch():

<script type="text/javascript">
function testFetch( method ) {
fetch( "./api.cfm", { method } );
}
</script>
<button onclick="testFetch( 'GET' )">GET</button>
<button onclick="testFetch( 'POST' )">POST</button>
<button onclick="testFetch( 'PUT' )">PUT</button>
<button onclick="testFetch( 'PATCH' )">PATCH</button>
<button onclick="testFetch( 'DELETE' )">DELETE</button>
view raw index.cfm hosted with ❤ by GitHub

When I run this ColdFusion page and then click each button, here's what we see in Chrome's network activity - much to my surprise:

Chrome network activity showing that PUT, PATCH, and DELETE methods will be propagated through redirects when using the fetch() API.

As you can see, both GET and POST requests result in a subsequent GET method during a 302 redirect. This lines up with my mental model for the web; and, from my previous sanity check. But, when the fetch() API makes a PUT, PATCH, or DELETE request, we can see that this HTTP method is propagated through the redirect.

This blows my mind. And, it's also another reminder to me that the web just makes more sense when you limit your HTTP methods to GET and POST; and following the POST/Redirect/GET pattern of interaction.

Want to use code from this post? Check out the license.

Reader Comments

16,020 Comments

Ahh, then I like! :D Especially if it's an easy change. If this required a lot of work, I am not sure if it would be worthwhile.

16,020 Comments

I'm all for it! Thanks for your continuous efforts to improve the platform. You are synonymous with the Lucee ecosystem!

Post A Comment — I'd Love To Hear From You!

Markdown formatting: Basic formatting is supported: bold, italic, blockquotes, lists, fenced code-blocks. Read more about markdown syntax »
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.
Cancel
I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel