import * as React from 'react'; import { fetchJson } from './fetch'; export default { title: 'ra-core/dataProvider/fetch', }; export const FetchJson = () => { const [token, setToken] = React.useState('secret'); const [record, setRecord] = React.useState(''); const [headerName, setHeaderName] = React.useState('X-Custom-Header'); const [headerValue, setHeaderValue] = React.useState('foobar'); const user = { token: `Bearer ${token}`, authenticated: !!token }; const getHeaders = () => { const headers = new Headers(); if (headerName) headers.set(headerName, headerValue); return headers; }; const doGet = () => { fetchJson('https://jsonplaceholder.typicode.com/posts/1', { user, headers: getHeaders(), }).then(({ status, headers, body, json }) => { console.log('GET result', { status, headers, body, json }); setRecord(body); }); }; const doPut = () => { fetchJson('https://jsonplaceholder.typicode.com/posts/1', { method: 'PUT', body: record, user, headers: getHeaders(), }).then(({ status, headers, body, json }) => { console.log('PUT result', { status, headers, body, json }); setRecord(body); }); }; return (

Tip: Open the DevTools network tab to see the HTTP Headers
Tip: Open the DevTools console tab to see the returned values

setToken(e.target.value)} style={{ flexGrow: 1 }} title="Clear this field to simulate an unauthenticated user" />
setHeaderName(e.target.value)} style={{ flexGrow: 1, marginRight: 10 }} title="Clear this field to remove the header" /> setHeaderValue(e.target.value)} style={{ flexGrow: 1, minWidth: 100 }} />