Skip to content

Remove form encoded object body. - #30

Merged
dgraham merged 4 commits into
masterfrom
remove-form-encode
Nov 3, 2014
Merged

dgraham merged 4 commits into
masterfrom
remove-form-encode

Conversation

@dgraham

@dgraham dgraham commented Nov 1, 2014

Copy link
Copy Markdown
Contributor

This was based on a misreading of the URLSearchParams portion of section 5.2 of https://fetch.spec.whatwg.org/#body-mixin.

Form encoding an Object doesn't work for keys that occur several times in one request body, like issues[]=12&issues[]=42. Use FormData as the request body to POST form fields instead.

/cc @annevk on spec compliance.

This was based on a misreading of the URLSearchParams portion of
section 5.2 of https://fetch.spec.whatwg.org/#body-mixin.

Use FormData as the request body to POST form fields instead.
@dgraham

dgraham commented Nov 1, 2014

Copy link
Copy Markdown
Contributor Author

If the site has jQuery available, but not FormData, this can be used to post a form with fetch:

fetch(form.getAttribute('action'), {
  method: form.getAttribute('method'),
  body: $.param($(form).serializeArray()),
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  }
)

The body must be consumed through the `text`, `json`, `formData`, or
`blob` functions.
@annevk

annevk commented Nov 2, 2014

Copy link
Copy Markdown

I'm not sure I understand what you're saying. URLSearchParams supports duplicate keys.

@dgraham

dgraham commented Nov 2, 2014

Copy link
Copy Markdown
Contributor Author

URLSearchParams works fine. This branch removes this:

fetch('/query', {
  method: 'post',
  body: {
    name: 'Hubot',
    name: 'Duplicate Hubot',
    login: 'hubot'
  }
})

I misread the spec, thinking that fetch accepted an Object as the body.

dgraham added a commit that referenced this pull request Nov 3, 2014
Remove form encoded object body.
@dgraham
dgraham merged commit 2232672 into master Nov 3, 2014
@dgraham
dgraham deleted the remove-form-encode branch November 3, 2014 01:46
dgraham added a commit that referenced this pull request Nov 13, 2014
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants