Handling a 302 redirect after POST

222 views
Skip to first unread message

John D Wells

unread,
May 22, 2020, 5:00:02 PM5/22/20
to unpoly
I'm trying to integrate Unpoly with a Craft CMS Commerce build, particularly using Unpoly to let the user make modifications to their cart, for things like changing item quantities, or applying a discount code.

If we take the discount code as an example, I have a simple form that POSTs the `couponCode` field to an action endpoint, which if successful then redirects the user back to the same page, with the code applied to the cart. Technically, I have the ability to change the redirect destination to anything I please, but in this use case, it redirects back to the same page.

If there is an error while attempting to add the coupon, for example it's invalid, then the response is a 200, rendering the same cart back, where I'm able to display the validation errors.

When I wire up this form to be submitted with Unpoly, it handles the errors just fine, but if the submission is successful, Unpoly doesn't refresh the UI.

When I look in the Console, with unpoly logging enabled, it says "Fatal error during request". Seems it's choking on the 302 redirect for some reason. I've tried adding the `X-Up-Location` header, but that doesn't seem to help.

Any advice or hints would be hugely appreciated. Thanks!

- John

Keyur Shah

unread,
May 22, 2020, 5:37:42 PM5/22/20
to unpoly
Hey,

I had a similar issue. Please see through here

https://groups.google.com/forum/m/#!topic/unpoly/tn2dGRGDOlk

Basically craft needs ‘data type: json’ as a header

I forgot exactly why but it’s because craft treats Ajax requests as json. And without that header, craft doesn’t seem to handle the request appropriately.

Please try that and let me know if it works out.

John D Wells

unread,
May 22, 2020, 5:39:08 PM5/22/20
to unpoly
Thanks Keyur! I'll try that out and report back.

John D Wells

unread,
May 22, 2020, 5:44:11 PM5/22/20
to unpoly
Actually - Keyur I'm not sure that's really what I'm after... Because I _want_ Craft to return an HTML response, because I then want Unpoly to replace the "summary" fragment of my page.

I've bound Unpoly to the form with `<form method="post" up-target="#unpoly-summary">`, where the `#unpoly-summary` is the ID container for the summary panel.  And FWIW, the coupon code form is inside this summary container.

HTH,
John

John D Wells

unread,
May 22, 2020, 6:07:11 PM5/22/20
to unpoly
I should also mention - this is with the brand new 0.62 release.

- John

Henning Koch

unread,
May 23, 2020, 10:37:14 AM5/23/20
to unp...@googlegroups.com
Hi John,

I encourage everyone looking for solutions on this list to use
https://glitch.com/ to create a minimal app demonstrating your issue.

I did this for your case here:
https://glitch.com/edit/#!/husky-descriptive-bonsai

As you can see Unpoly will handle the 302 redirect. Something else must
be wrong.

Best regards
Henning
> --
> You received this message because you are subscribed to the Google
> Groups "unpoly" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to unpoly+un...@googlegroups.com
> <mailto:unpoly+un...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/unpoly/9c8d4729-451b-413d-9bf6-cb802627c552%40googlegroups.com
> <https://groups.google.com/d/msgid/unpoly/9c8d4729-451b-413d-9bf6-cb802627c552%40googlegroups.com?utm_medium=email&utm_source=footer>.

John D Wells

unread,
May 24, 2020, 8:20:03 AM5/24/20
to unpoly
Hi Henning,

I really appreciate your reply!

So I've figured it out - and actually the clue was in Keyur's similar issue linked above. This perhaps is merely an idiosyncrasy with Craft (or actually Yii2, the framework it's built on), but I'm going to explain what was happening, in case there is anything that Unpoly could do to handle this scenario...

When Craft Commerce handles a POST submission and decides how to marshal the response, it first checks if the response will "accept JSON". If it does, it returns JSON; otherwise, it initiates a 302 redirect, and that is all done via Yii2's internal redirection routine...

However, Yii2 does it's own check for how best to return the response, and as part of that routine decides if the request "is AJAX", which it does by checking if it's an XMLHttpRequest. If it is, then it sends an `X-Redirect` header (or if it further determines it is Pjax, sends an `X-Pjax-Url` header). Otherwise, it sets the `Location` header.

What this meant was that, Yii2 was not sending a "Location" header back with its 302 response, because Yii2 detected that it was an XMLHttpRequest.

I tried adding "X-Up-Location" as a header, but it seemed that without also the "Location" header, the redirect was essentially going unhandled, and so then unpoly throws that fatal error because it sees that the response is not in the 200 range.

I can fix this on my end, by ensuring that a "Location" header gets sent; but I'd be curious to hear your thoughts on the above. Is it a surprise that "X-Up-Location" wasn't enough to get this working? Is "X-Redirect" a convention that should be accounted for?

Thanks for your time!




Henning Koch

unread,
May 24, 2020, 8:41:51 AM5/24/20
to unp...@googlegroups.com
Hi John,

thanks for shining more light on this.

I've also seen other libraries detecting AJAX requests and being "smart"
about responding. Because of this, the next major version of Unpoly will
no longer send the `X-Requested-With: XMLHttpRequest` header which
server-side libraries use to detect AJAX requests.

Note that there might be misunderstanding about the purpose of the
`X-Up-Location` header. This header can *not* be used to trigger a
redirect. If you want to redirect, use a standard HTTP 302 status with a
standard `Location` header.

Server responses may use `X-Up-Location` to update the browser's
location bar with a different URL than the actual request's URL. I don't
think it is relevant to your issue.

Best regards
Henning


Am 24.05.2020 um 14:20 schrieb John D Wells:
> Hi Henning,
>
> I really appreciate your reply!
>
> So I've figured it out - and actually the clue was in Keyur's similar
> issue linked above. This perhaps is merely an idiosyncrasy with Craft
> (or actually Yii2, the framework it's built on), but I'm going to
> explain what was happening, in case there is anything that Unpoly could
> do to handle this scenario...
>
> When Craft Commerce handles a POST submission and decides how to marshal
> the response, it first checks if the response will "accept JSON". If it
> does, it returns JSON; otherwise, it initiates a 302 redirect, and that
> is all done via Yii2's internal redirection routine...
>
> However, Yii2 does it's /own/ check for how best to return the response,
> > an email to unp...@googlegroups.com <javascript:>
> > <mailto:unp...@googlegroups.com <javascript:>>.
> <https://groups.google.com/d/msgid/unpoly/9c8d4729-451b-413d-9bf6-cb802627c552%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/unpoly/9c8d4729-451b-413d-9bf6-cb802627c552%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "unpoly" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to unpoly+un...@googlegroups.com
> <mailto:unpoly+un...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/unpoly/b5d7e28b-5c23-4e0d-ba7d-a935ac4fdbcd%40googlegroups.com
> <https://groups.google.com/d/msgid/unpoly/b5d7e28b-5c23-4e0d-ba7d-a935ac4fdbcd%40googlegroups.com?utm_medium=email&utm_source=footer>.

John D Wells

unread,
May 25, 2020, 8:19:40 AM5/25/20
to unpoly
Hi Henning,

Thanks for considering this scenario. Removing that header seems like a significant breaking change - yet since Unpoly is essentially mimicking a "normal" request, it's probably in the right direction. Perhaps adding a "X-Up-Requested-With" header or similar might be a useful thing to send in its place? Just thinking aloud.

Thanks also for clarifying the `X-Up-Location`.

Cheers,
John

Henning Koch

unread,
May 25, 2020, 8:57:19 AM5/25/20
to unp...@googlegroups.com
Hi John,

even with X-Requested-With removed, servers may still detect an Unpoly
request by looking for an X-Up-Target header.

There are several libraries already using the X-Up-Target header for
that purpose. I know of unpoly-rails (Ruby on Rails), roda-unpoly
(Roda), ex_unpoly (Elixir/Phoenix) and php-unpoly (Symfony). I don't
think there's one for Yii yet.

Best regards
Henning
> https://groups.google.com/d/msgid/unpoly/b5d7e28b-5c23-4e0d-ba7d-a935ac4fdbcd%40googlegroups.com
> <https://groups.google.com/d/msgid/unpoly/b5d7e28b-5c23-4e0d-ba7d-a935ac4fdbcd%40googlegroups.com>
>
> >
> <https://groups.google.com/d/msgid/unpoly/b5d7e28b-5c23-4e0d-ba7d-a935ac4fdbcd%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/unpoly/b5d7e28b-5c23-4e0d-ba7d-a935ac4fdbcd%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "unpoly" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to unpoly+un...@googlegroups.com
> <mailto:unpoly+un...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/unpoly/a2b3c4df-8c75-450b-b551-5bba306cd37c%40googlegroups.com
> <https://groups.google.com/d/msgid/unpoly/a2b3c4df-8c75-450b-b551-5bba306cd37c%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages