Swizec Teller - a geek with a hatswizec.com

    How to waste hours of life with fetch() and a bit of brainfart

    Today, I had a dumb idea: "Nested superagent calls sure look messy… maybe I should use fetch() instead…”

    What happened next was a little bit of this:

    Followed by a lot of this:

    In case you don't know, superagent is a popular library for executing AJAX requests in JavaScript. Clean API, works well, makes life easy. I've been a fan for years.

    fetch() is a new JavaScript API for the same. Clean API, Promise-based interface, solves a problem you thought was solved. Not yet an official standard, but supported in all modern browsers. You can use it in production code, if you're compiling with Babel and enable babel-polyfill.

    Both superagent and fetch() enable you to talk to a server. The first produces clean code that gets nesty if you need many things. The second produces clean code that is Promis-y and sometimes cumbersome.

    For example, to get a JSON object with superagent, you'd do this:

    superagent.get('/api/some.json')
    .set('Accept, 'application/json')
    .end((err, res) => {
    // res.body contains parsed JSON
    });

    The same call with fetch() looks like this:

    fetch("/api/some.json", { headers: { Accept: "application/json" } })
    .then((res) => res.json())
    .then((json) => {
    // json contains parsed JSON
    })

    Both just 5 lines of code. It’s debatable which is cleaner. They look the same to me. ¯_(ツ)_

    Curated Fullstack Web Essays

    Get a series of curated essays on Fullstack Web development. Lessons and insights from building software for production. No bullshit.

    Join over 14,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

    Where it gets interesting is when you have two calls that rely on each other. Observe:

    superagent.get('/api/some.json')
    .set('Accept, 'application/json')
    .end((err, res) => {
    const url = `/api/details/${res.body.details_id}.json`;
    superagent.get(url)
    .set('Accept', 'application/json')
    .end((err, res) => {
    // res.body contains parsed details
    });
    });

    Vs.

    fetch("/api/some.json", { headers: { Accept: "application/json" } })
    .then((res) => res.json())
    .then((json) => {
    const url = `/api/details/${json.details_id}.json`
    return fetch(url, { headers: { Accept: "application/json" } })
    })
    .then((res) => res.json())
    .then((json) => {
    // json contains parsed details
    })

    The promises approach does look cleaner ?

    But there's a catch with fetch()

    See the Accept header? Superagent sends it as an Accept header. Fetch sends it as accept.

    Your clean code stops working. You're doing everything right: you send the Accept header, you send the Authorization header for your API's token-based authentication, and yet…

    Swizec Teller published ServerlessHandbook.dev avatarSwizec Teller published ServerlessHandbook.dev@Swizec
    TIL: Rails doesn't think HTTP headers are case-insensitive.

    fetch() knows they are and forces lowercase
    Tweet media

    You fall into a rabbit hole…

    Swizec Teller published ServerlessHandbook.dev avatarSwizec Teller published ServerlessHandbook.dev@Swizec
    Rails can still get the token out of Authentication header, but returns 401 anyway.

    This is what I get for trying to uses Promises.

    You read through all of the relevant questions and answers on StackOverflow. You google and google. You spelunk through Rails's code on Github.

    You abandon all hope…

    Swizec Teller published ServerlessHandbook.dev avatarSwizec Teller published ServerlessHandbook.dev@Swizec
    Oh great, looks like it's actually Warden bug deep inside the bowels of Devise.

    All I wanted was to use fetch() 😭

    You're about ready to start throwing things. Nothing makes sense, this is dumb, everything sucks, all you wanted was to Do The Right Thing™ and now you're stuck debugging huge frameworks.

    ?

    By the way, Devise is a library for user authentication, and Warden is the core authentication library it wraps. No, I don't know why this happens in two libraries. Maybe historical reasons.

    And then it hits you. You're being an idiot.

    The difference between superagent and fetch() isn't that one sends your headers as-given and the other lowercases their names. The difference is that superagent sends a cookie and fetch() doesn't!

    Swizec Teller published ServerlessHandbook.dev avatarSwizec Teller published ServerlessHandbook.dev@Swizec
    Oh god ... the REAL issue is that fetch() doesn't automatically send Cookies and I didn't notice.

    Fuck my life.
    Tweet media

    Your API relies on tokens to authenticate the client, and on session cookies to identify users. ? It makes total sense, super common design. But ugh!!

    You have to add credentials: 'same-origin' to your fetch() settings object. Then it works. The API talks to you, the JavaScript client does its thing, users are happy.

    Code looks like this:

    fetch("/api/some.json", {
    headers: { Accept: "application/json" },
    credentials: "same-origin",
    })
    .then((res) => res.json())
    .then((json) => {
    const url = `/api/details/${json.details_id}.json`
    return fetch(url, {
    headers: { Accept: "application/json" },
    credentials: "same-origin",
    })
    })
    .then((res) => res.json())
    .then((json) => {
    // json contains parsed details
    })

    In real code, I suggest wrapping fetch() in a helper function that always adds API-specific options like Accept and Authorization headers and credentials. Your future self will thank you.

    No, I don't know why fetch() breaks the 20-year old convention that cookies are automatically included in requests.

    Did you enjoy this article?

    Published on November 1st, 2016 in Technical, JavaScript, Fullstack Web

    Want to become a Fullstack Web expert?

    Learning from tutorials is great! You follow some steps, learn a smol lesson, and feel like you got this. Then you go into an interview, get a question from the boss, or encounter a new situation and o-oh.

    Shit, how does this work again? 😅

    That's the problem with tutorials. They're not how the world works. Real software is a mess. A best-effort pile of duct tape and chewing gum. You need deep understanding, not recipes.

    Leave your email and get the Fullstack Web Essays series - a series of curated essays and experiments on modern Fullstack Web development. Lessons learned from practice building production software.

    Curated Fullstack Web Essays

    Get a series of curated essays on Fullstack Web development. Lessons and insights from building software for production. No bullshit.

    Join over 14,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

    Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

    Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

    Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

    Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

    Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

    By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️

    Created bySwizecwith ❤️