首页 > 解决方案 > How to create or update file using Github content api

问题描述

I wanna update or create file (such as ~~.yml, ~~.json) with Github content API. So I followed directions written in the github docs, it says to request PUT method for update or create. I wrote code like below, but I got 404 error,,

    axios({
      method: 'put',
      url:
        'https://api.github.com/repos/{owner}/{repo}/contents/package.json',
      data: {
        message: 'put method test',
        content: { newData },
        sha: { sha },
      },
    }).then((res) => console.log(res));

There is encoded code in the {content} btoa(content)

标签: apigithub

解决方案


404 probably means an access right issue: a PUT method needs for you to be properly authenticated in order for GitHub to determine if you have the right to put anything in that repository.
See "Other authentication methods", using a PAT (Personal access token) and a dedicated "Authorization: token TOKEN" header

And you need to replace {owner}/{repo} by the actual owner GitHub account name, and GitHub repository name: those are placeholders.


推荐阅读