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

Add configs to choose between ecoding parameters in the URL or request body #1525

Merged
merged 8 commits into from
Jul 10, 2023

Conversation

alexpetros
Copy link
Collaborator

@alexpetros alexpetros commented Jun 29, 2023

Summary

Even though the semantics of request bodies for GET are undefined, it is legal to set request bodies for them. By default, GET form parameters should be encoded as URLs. If, however, an extension defines encodeParameters, that should override the default and set the request bodies for GET methods, just like it does for all the other HTTP methods.

Resolves: #1514, #1539

Testing

Wrote some new unit tests for GET requests that have json-enc enabled, and this PR fixes the broken DELETE one.

Even though the semantics of request bodies for GET and DELETE are
undefined, it is legal to set request bodies for them. By default, GET
and DELETE form parameters should be encoded as URLs. If, however, an
extension defines `encodeParameters`, that should override the default
and set the request bodies for GET and DELETE methods, just like it does
for all the other HTTP methods.
@alexpetros alexpetros force-pushed the get-and-delete-body-extension branch from b9403d8 to 0baa329 Compare July 4, 2023 20:59
@alexpetros alexpetros requested a review from 1cg July 4, 2023 21:00
@alexpetros alexpetros changed the title Allow extensions to set request bodies for GET and DELETE Allow extensions to set request bodies for GET Jul 4, 2023
src/htmx.js Show resolved Hide resolved
@alexpetros alexpetros requested a review from 1cg July 6, 2023 18:38
@alexpetros
Copy link
Collaborator Author

@1cg updated and ready for review

@alexpetros alexpetros changed the title Allow extensions to set request bodies for GET Make it configurable to use URL parameters or request bodies Jul 6, 2023
@alexpetros alexpetros changed the title Make it configurable to use URL parameters or request bodies Add configs to choose between ecoding parameters in the URL or request body Jul 6, 2023
src/htmx.js Outdated
useUrlParams = false
}
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think we can drop this now? I don't like signaling this via the presence of a function, would rather the extension do so via the htmx:configRequest event if necessary...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup that's smart, done.

@1cg 1cg merged commit febfa1c into bigskysoftware:dev Jul 10, 2023
5 checks passed
@1cg
Copy link
Contributor

1cg commented Jul 10, 2023

thx @alexpetros!

@jantari
Copy link

jantari commented Mar 10, 2024

So I found this PR (and related issues) after searching for why my Go backend didn't accept the form data that HTMX sends in a DELETE request.

According to this stackoverflow post: https://stackoverflow.com/a/59011736

Sending a body in a DELETE request is non-standard and cannot be expected to work. Indeed the body is ignored by Golangs request.ParseForm() method.

It looks like this PR didn't just make the behavior of DELETE requests configurable, which is fine, but also changed their default behavior from the normal and preferred way of encoding parameters in the URL to sending them in the body. This is now an issue for Golang users such as myself, and surely for other languages or libraries as well.

Am i misinterpreting something or should the default behavior for DELETEs be reverted back to encoding in URL query params?

@alexpetros
Copy link
Collaborator Author

It looks like this PR didn't just make the behavior of DELETE requests configurable, which is fine, but also changed their default behavior from the normal and preferred way of encoding parameters in the URL to sending them in the body. This is now an issue for Golang users such as myself, and surely for other languages or libraries as well.

Am i misinterpreting something or should the default behavior for DELETEs be reverted back to encoding in URL query params?

Yes, you're wrong about the second part—htmx has always sent DELETE bodies by default, that's what inspired the PR in the first place. This default is being changed in 2.0 to the behavior that you're requesting, but in the meantime, you can configure it using the config that was implemented here.

@jantari
Copy link

jantari commented Mar 11, 2024

Thank you for clarifying, I just misinterpreted the diff here. I implemented the config to mitigate the issue.

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

Successfully merging this pull request may close these issues.

3 participants