{"id":47639,"library":"fetch-rails","title":"fetch-rails","description":"A lightweight (4kB) wrapper around the GitHub fetch polyfill that automatically includes Rails CSRF tokens in requests. Current version 0.8.3 (last release 2019, low activity). Simplifies JSON, HTML, and text requests for Rails apps by handling CSRF token extraction, parameter encoding, and HTTP status checks. Compared to plain fetch or axios, it provides Rails-specific defaults like JSON headers and credential inclusion.","status":"maintenance","version":"0.8.3","language":"javascript","source_language":"en","source_url":"https://github.com/MiguelSavignano/fetch-rails","tags":["javascript","fetch","rails","csrf token"],"install":[{"cmd":"npm install fetch-rails","lang":"bash","label":"npm"},{"cmd":"yarn add fetch-rails","lang":"bash","label":"yarn"},{"cmd":"pnpm add fetch-rails","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"polyfill for fetch API in older browsers (IE 9+) – intended as peer dependency","package":"whatwg-fetch","optional":true}],"imports":[{"note":"ESM default export; CJS require gives the default object, not a named export.","wrong":"const { Fetch } = require('fetch-rails')","symbol":"default (Fetch)","correct":"import Fetch from 'fetch-rails'"},{"note":"Methods are on the default export, not separate named exports.","wrong":"import { json } from 'fetch-rails'","symbol":"Fetch.json","correct":"import Fetch from 'fetch-rails'\nFetch.json(url, options)"},{"note":"The method expects data as second argument, not a body option.","wrong":"Fetch.postJSON(url, { body: data })","symbol":"Fetch.postJSON","correct":"import Fetch from 'fetch-rails'\nFetch.postJSON(url, data)"}],"quickstart":{"code":"import Fetch from 'fetch-rails';\n\n// GET JSON\nFetch.json('https://jsonplaceholder.typicode.com/posts/1')\n  .then(post => console.log(post))\n  .catch(err => console.error(err));\n\n// POST JSON\nFetch.postJSON('https://jsonplaceholder.typicode.com/posts', {\n  title: 'foo',\n  body: 'bar',\n  userId: 1\n}).then(post => console.log(post)).catch(err => console.error(err));\n\n// With Rails CSRF auto-inclusion\nFetch.json('/api/resource', { search: { q: 'term' } })\n  .then(data => console.log(data));","lang":"javascript","description":"Shows basic GET and POST JSON requests including CSRF token handling for Rails backends."},"warnings":[{"fix":"Use native fetch with rails-ujs CSRF tokens, or switch to axios with csrf interceptors.","message":"The package is no longer actively maintained; last release was in 2019. Consider using modern alternatives or maintain a fork.","severity":"deprecated","affected_versions":">=0.8.3"},{"fix":"Install whatwg-fetch and import it before fetch-rails in your entry file.","message":"The package does not include a fetch polyfill; you must provide it separately (e.g., whatwg-fetch) for older browsers like IE 9.","severity":"gotcha","affected_versions":"*"},{"fix":"Ensure your Rails layout includes <%= csrf_meta_tags %> and that the meta tag name is 'csrf-token'.","message":"CSRF token extraction relies on a meta tag named 'csrf-token' in the DOM. If the tag is missing or named differently, requests may fail.","severity":"gotcha","affected_versions":"*"},{"fix":"Use ESM (import) or transpile with Babel/TypeScript.","message":"The package uses ES modules (import/export) but does not provide a CommonJS build. Direct require() in Node.js will fail.","severity":"breaking","affected_versions":">=0.8.0"}],"env_vars":null,"search_vec":"'0.8.3':23 '2019':26 '4kb':6 'activ':28 'app':37 'around':8 'automat':14 'axio':54 'check':48 'compar':49 'credenti':65 'csrf':17,40,70 'current':21 'default':60 'encod':44 'extract':42 'fetch':2,11,52,68 'fetch-rail':1 'github':10 'handl':39 'header':63 'html':31 'http':46 'includ':15 'inclus':66 'javascript':67 'json':30,62 'last':24 'lightweight':5 'like':61 'low':27 'paramet':43 'plain':51 'polyfil':12 'provid':56 'rail':3,16,36,58,69 'rails-specif':57 'releas':25 'request':20,34 'simplifi':29 'specif':59 'status':47 'text':33 'token':18,41,71 'version':22 'wrapper':7","created_at":"2026-06-07T16:52:32.244962+00:00","updated_at":"2026-06-07T16:52:32.244962+00:00","problems":[{"fix":"Ensure a global fetch polyfill (like whatwg-fetch) is loaded and not shadowed by a local variable named fetch.","cause":"fetch-rails relies on the global fetch function, which may be overwritten or unavailable in certain environments (e.g., using a polyfill that isn't properly bound).","error":"Uncaught TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation"},{"fix":"Insert <%= csrf_meta_tags %> in your Rails layout file (app/views/layouts/application.html.erb).","cause":"The meta tag with name 'csrf-token' is not present in the DOM when fetch-rails tries to read the CSRF token.","error":"Uncaught TypeError: Cannot read property 'getAttribute' of null"},{"fix":"Use ES module import: import Fetch from 'fetch-rails'. If needed, use a bundler like Webpack or Browserify.","cause":"Using require('fetch-rails') in a browser environment that doesn't support CommonJS modules.","error":"Uncaught ReferenceError: require is not defined"},{"fix":"Ensure proper import order; import Fetch from 'fetch-rails' before using Fetch methods.","cause":"Trying to call Fetch.json before the import completes (e.g., due to circular dependencies or incorrect import).","error":"Uncaught TypeError: Fetch.json is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/MiguelSavignano/fetch-rails#readme","github":"https://github.com/MiguelSavignano/fetch-rails","docs":null,"changelog":null,"pypi":null,"npm":"fetch-rails","openapi_spec":null,"status_page":null,"smithery":null,"categories":["security"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}