dom-examples icon indicating copy to clipboard operation
dom-examples copied to clipboard

fetch-with-init-then-request sets Content-Type request header for GET

Open sideshowbarker opened this issue 8 years ago • 7 comments

https://github.com/mdn/fetch-examples/blob/7a20c41cb1d65c5e8ae9eae3270caa2dadadf12e/fetch-with-init-then-request/index.html#L25-L36 That example is setting a Content-Type request header for a GET request, which serves no real purpose since the request has no request body/payload.

I guess a simple fix would be to change the request header to an Accept header. But it seems like that wouldn’t be serving much real purpose either, since the request for flowers.jpg will without any Accept request header needed return that JPEG file as expected anyway.

A more real-world example would be a POST request with a JSON request payload/body and a Content-Type: application/json request header. But I don’t know if/how that would fit with how the set of examples here are served.

Regardless, I’m happy to help work on putting together another example that illustrates the same thing this one’s intended to illustrate.

But note also that whatever changes we make here, we will also need to update the example code at https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Example, which repeats the code from here.

sideshowbarker avatar Oct 06 '17 23:10 sideshowbarker

Hi @sideshowbarker! I'm sorry it has taken me, like, 4 months to reply to this. It just slipped out of view at the bottom of my inbox. ;-(

I'd love it if you could throw together an improved example for this. I think the post example with some simple JSON would be the best route to take. I can update the necessary MDN pages.

chrisdavidmills avatar Feb 20 '18 09:02 chrisdavidmills

Thanks @chrisdavidmills — is there a maintained endpoint I can POST some JSON to as part of the demo (and get a useful response back from)?

sideshowbarker avatar Feb 21 '18 08:02 sideshowbarker

@sideshowbarker Oh gawd, I don't know. I'll ask around.

chrisdavidmills avatar Feb 21 '18 08:02 chrisdavidmills

Would http://www.jsontest.com/ be any use?

chrisdavidmills avatar Feb 21 '18 09:02 chrisdavidmills

Would http://www.jsontest.com/ be any use?

Yes — thanks much, will use that

sideshowbarker avatar Feb 21 '18 10:02 sideshowbarker

This one also looks pretty useful: http://httpbin.org/

chrisdavidmills avatar Feb 21 '18 18:02 chrisdavidmills

We are in the process of merging some of our smaller repositories into our larger, more well-maintained repositories. This repository is being moved to the dom-examples repository. While there were some updates to the file in question, the issue raised here still exists. Instead of closing this issue, I am going to transfer it to the dom-examples repo, where we can continue the conversation.

schalkneethling avatar Aug 15 '22 19:08 schalkneethling

Hey @sideshowbarker 👋🏻

I was looking at this one again recently and thinking of what might be interesting to show. My first thoughts are Authorization header, but we're running the example, so this would fail. What about doing something with caching?

const myImage = document.querySelector("img");

const reqHeaders = new Headers();

// reqHeaders.append("If-Modified-Since", "Fri, 1 Sep 2023 09:30:00 GMT");
// Accept is also not really necessary
// reqHeaders.append("Accept", "image/jpeg");
reqHeaders.set("Cache-Control", "max-age=604800");

const options = {
  headers: reqHeaders,
};

const req = new Request("flowers.jpg");

fetch(req, options)
  .then((response) => {
    if (!response.ok) {
      throw new Error(`HTTP error, status = ${response.status}`);
    }
    return response.blob();
  })
  .then((blob) => {
    const objectURL = URL.createObjectURL(blob);
    myImage.src = objectURL;
  })
  .catch((error) => {
    var p = document.createElement("p");
    p.appendChild(document.createTextNode(`Error: ${error.message}`));
    document.body.insertBefore(p, myImage);
  });

If-Modified-Since is good, but if we get a 304, then we have to do some other handling, which complicates it a little. What do you think?

bsmth avatar Apr 10 '24 10:04 bsmth

Yeah, I think an example with a Cache-Control request header would be correct, and a big improvement

sideshowbarker avatar Apr 10 '24 13:04 sideshowbarker

Great, I've assigned myself, I can tag you for a review if you'd like to have a look

bsmth avatar Apr 10 '24 15:04 bsmth