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

Support ordered form data #391

Closed
rosahaj opened this issue Oct 6, 2024 · 3 comments
Closed

Support ordered form data #391

rosahaj opened this issue Oct 6, 2024 · 3 comments

Comments

@rosahaj
Copy link
Contributor

rosahaj commented Oct 6, 2024

At the moment form data is always sorted alphabetically, because map and net/url.Values sort keys alphabetically (see this issue). However, most browsers send form data in the order in which it appears in the submitted form. This poses an issue when attempting to mimic browser behavior (using client.ImpersonateChrome or client.ImpersonateFirefox) as alphabetically ordered form data can be detected as anomalous, particularly when every other request is honoring the original form order.

My suggestion would be to add request.SetOrderedFormData and client.SetCommonOrderedFormData methods, which accept an ordered map (for example this one) as an argument and set the OrderedFormData field of the request or client respectively. Unfortunately this could create confusion over whether FormData or OrderedFormData takes precedence when both are set. Changing the type of the FormData field from net/url.Values to an ordered map would solve this problem, but would break things for anyone relying on FormData to be of the type net/url.Values.

@imroc
Copy link
Owner

imroc commented Oct 10, 2024

Good idea, will implement it when I have time

imroc added a commit that referenced this issue Oct 10, 2024
@imroc
Copy link
Owner

imroc commented Oct 10, 2024

Released in v3.47.0

Runnable demo:

package main

import (
	"github.com/imroc/req/v3"
)

func main() {
	req.DevMode()
	req.R().
		// EnableForceMultipart().
		SetOrderedFormData(
			"key3", "value3",
			"key1", "value1",
			"key2", "value2",
		).Post("https://httpbin.org/post")
}

@imroc imroc closed this as completed Oct 10, 2024
@rosahaj
Copy link
Contributor Author

rosahaj commented Oct 12, 2024

Thanks, that was really quick!

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

2 participants