Missing `forEach` method on `Headers`

While the documentation states that the Headers class matches the documentation provided by MDN, the class actually misses the forEach method.

This is not breaking, as one can easily polyfill the method, but this should be fixed, as some packages may use this method, which would result in errors.

3 Likes

Thanks for the report! Iā€™m not sure when weā€™ll have time to add this, but itā€™s on our radar.

2 Likes

Thanks! Iā€™ll get this updated in our documentation ASAP.

1 Like

We also just got caught out by this, itā€™s exposed via the types so would be nice to have it implemented.

Apologies to revive that topic, but this has been almost two years nowā€¦ any expected release dates?

We rolled this out early this week, you should now be able to use forEach on Headers, FormData, and URLSearchParams :slight_smile:

Does this mean there is FormData support now? Or just for iteration?

Iā€™m not sure what youā€™re asking - we already supported FormData before this week. If it helps, this is a snippet from the test case:

  let fd = new FormData();

  fd.forEach(function(v, k, t) {
    assert(false); // should not be called on empty form
  });

  let foreachOutput = [];
  fd.append("key1", "value1");
  fd.append("key2", "value2");

  fd.forEach(function(value, key, captureFd) {
    foreachOutput.push(`${key}=${value}`);
  });

  console.assert("" + foreachOutput.join('&'), "key1=value1&key2=value2");

Yes, sorry, I wasnā€™t specific enough - File API support for FormData.

This is coming very soonā€¦ in fact itā€™s technically already there but weā€™re missing Wrangler-side support to opt into it. (We couldnā€™t just change the API for everyone because it could break people.) Should be ready in the next couple weeks.

2 Likes

Thatā€™s awesome Kenton, canā€™t wait to try it out. Perfect timing too :wink:

What about folks that arenā€™t using Wrangler - can I opt-into this today with an api call?

There is, but I probably shouldnā€™t give out the info before I get a chance to test it myself, since we might have to change it if there are issues. Iā€™ll let you know when weā€™re ready for people to use itā€¦

Fair enough, thanks!

Hi @thomas4 and @john.spurlock,

Sorry for the delay, there were indeed a few bugs I had to work through. But I just posted a Wrangler PR which adds support:

If you are using the upload API directly without Wrangler, the compatibility_date and compatibility_flags fields just go directly into the JSON metadata blob (the first ā€œpartā€ of the multipart/form-data upload bundle).

1 Like

Works via the api, thanks. Would love to see the longstanding buggy URL implementation finally fixed with an upcoming compat flag!

URL is the canonical example of something we want to fix with this approach. But itā€™s a bit more work as we actually have to write a compliant implementation. :slight_smile:

1 Like