I tried sending a file to my github repository via the html process but it failed with error 422. I feel like my numbers are good, but...no. I'm a beginner in this part and I can't find a solution to my problem.
In this article: https://levelup.gitconnected.com/how-to-upload-images-to-github-via-javascript-59163b8fff27
Someone wrote about splitting the data using base64
which could be a track but I don't know how to encode it in my case. Below are my attempts and failures! ! !
// REACT import React from "react" export default function ApiGithubToken() { const load = e => { if(e.target !== null) { let files = e.target.files; if(files.length > 0) { if(files[0] !== null) { const git= { owner : 'knupel', repo: 'gatsby_react_lab', path: `media/test/`, token: process.env.GATSBY_TOKEN_GITHUB, } upload_file(git, files[0]); } } } } return ( <div> <p>Sélectionner le fichier à déposer sur Github / Select your file to upload in Github</p> <input type="file" name="input" onChange={load}/> <br/> <br/> <button type="submit">CHARGEZ</button> </div> ) } const upload_file = async (git: { owner: string; repo: string; path: string; token: string | undefined; }, data: any) => { const res = await fetch(`https://api.github.com/repos/${git.owner}/${git.repo}/contents/${git.path}`, { method: "PUT", headers: { Accept: "application/vnd.github+json", Authorization: `Bearer ${git.token}` }, body: JSON.stringify({ message: "Téléversement de votre fichier", content: data // content: data.split('base64,')[1] // content: data.content }) } ); return await res.json(); }
and error message
I updated the code as you suggested @AKX but the error returned is the same error 422
Good thing, now the document is approved, I believe it feels good