実現したいこと
js
1const url = path => `https://api.github.com/${path}`; 2 3function Post(path, data) { 4 // path"gist"を渡してる 5 const headers = { 6 "Authorization": `token ${accessToken}`, 7 "Content-Type": "application/vnd.github.v3.text+json" 8 }; 9 10 return fetch(url(path), { 11 method: "POST", 12 headers: headers, 13 body: JSON.stringify(data) 14 }) 15 .then(res => { 16 res.json(); 17 }) 18 .then(payload => { 19 return { resp: payload }; 20 }) 21 .catch(error => { 22 // 404が返ってくる 23 return { error: error }; 24 }); 25}
という関数を用意し、
js
1const data = { 2 description: "test", 3 public: true, 4 files: { 5 "hello.rb": { 6 content: "hello" 7 } 8 } 9}; 10 11const { resp, error } = Post("gists", data);
といった感じでgistを投稿したいです。
できていないところ
上記のfetchを叩いても404が返ってきてしまいます。
{ "message": "Not Found", "documentation_url": "https://developer.github.com/v3/gists/#create-a-gist" }
できているところ
githubの認証自体はできています。OAuthでaccess_tokenを取得し、それを使ってGETを叩くことまではできました。
例えば、
js
1function Get(path) { 2 const headers = { 3 Authorization: `token ${accessToken}`, 4 }; 5 // 二箇所returnを書かないとPromiseが返らない 6 return fetch(url(path), { 7 method: "GET", 8 mode: "cors", 9 headers: headers 10 }) 11 .then(res => res.json()) 12 .then(payload => { 13 return { resp: payload }; 14 }) 15 .catch(error => { 16 return { error: error }; 17 }); 18} 19 20const { resp, error} = Get("user");
上記のコードでは認証したアカウントの情報を取得できるので認証のせいではないと考えています。
試したこと
headerの変更を試みたのですが、特に変化はありませんでした。(Content-Type
が怪しいのかなと思いました。)
また、curlで叩いても同様のエラーが出ました。
shell
1curl -X POST\ 2-H "Content-Type: application/json"\ 3-H "Authorization: token ACCESS_TOKEN"\ 4-d '{"description":"Test","public":true,"files":{"hello.rb":{"content":"hello world"}}'\ 5https://api.github.com/gists
// response { "message": "Not Found", "documentation_url": "https://developer.github.com/v3/gists/#create-a-gist" }
どちらでも同様のエラーが出ているので、POSTの仕方が悪いと思われます。(特に、GETとの違いを考えてrequest bodyが怪しいです。)
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。