Form Data and Fetch

by Bret Lowrey

HTML

<script src="https://wzrd.in/standalone/expect@latest"></script>
<body>
  Testing...
</body>

JavaScript

const sendData = (url, data, callback) => {
  var formData = new FormData();
  for (name in data) {
    formData.append(name, data[name]);
  }

  fetch(url, {
      method: 'POST',
      body: formData
    })
    .then(resp => resp.json())
    .then(json => callback(json));
}

const testData = {
  test: 'val'
};
sendData('https://httpbin.org/post', testData,
  resp => {
    expect(resp.form).toEqual(testData);
    document.body.innerHTML = "Passed test";
  });