Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AJAX acceleration does not support PUT HTTP method (only sends a POST) #51

Closed
acnebs opened this issue Jan 26, 2018 · 5 comments
Closed

Comments

@acnebs
Copy link

acnebs commented Jan 26, 2018

As far as I can tell, up.request(..., {method: 'PUT'}) sends a POST request instead of the desired PUT request, making the distinct handling of PUT/POST in our REST backend useless.

Is there a reason for this? Am I missing something obvious?

@adam12
Copy link
Member

adam12 commented Jan 26, 2018

Basically, browsers didn't (and might still) not support anything other than GET/POST (not including HEAD and OPTIONS). To work around this, Ruby on Rails started adding a _method parameter to all forms that weren't a GET or POST. This was adopted by other libraries and frameworks, and somewhat became the de-facto standard.

Unpoly works in the same way - the actual request is a POST, but with _method=PUT as a parameter.

If you're using Rack or something based off it, there is a Rack::MethodOverride middleware you can use to enable the _method param.

@acnebs
Copy link
Author

acnebs commented Jan 26, 2018

Ah, I didn't realize this was a layover from Rails.

However, browsers limiting themselves to GET/POST is only applicable to HTML forms. This is an AJAX request, so it is presumably using XMLHttpRequest under the hood, which does support the PUT method.

More to the point, jQuery's $.ajax() also supports the PUT method, and since Unpoly is built on top of jQuery, it seems sensible (consistent?) to include the method in its vanilla form, rather than being wrapped in a POST.

@adam12
Copy link
Member

adam12 commented Jan 26, 2018

Yes , it was form specific. The original premise was that without Javascript, people still expected forms to submit correctly (ala, the original button_to helper in Rails).

Since Unpoly wouldn't be used without Javascript enabled, I agree that Unpoly should use the native method where possible.

But looking at the source code, it looks like this is already the case, except Unpoly makes the decision to wrap non-GET/POST methods into a POST, as you're seeing. You just need to tell Unpoly not to wrap them in a POST.

Spitballing here, but you could try this:

up.proxy.config.wrapMethods = []

@acnebs
Copy link
Author

acnebs commented Jan 26, 2018

Ah, I looked into the source enough to realize that methods were being wrapped, but not closely enough to see that solution.

It worked perfectly, thank you @adam12!

@acnebs acnebs closed this as completed Jan 26, 2018
@triskweline
Copy link
Contributor

Also note that while many browsers now support verbs like PUT, PATCH, they handle redirect responses in an unexpected way.

That's another reason why many frameworks like Rails, Symfony and Express offer a way to wrap the verb in a _method param.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants