User-Agent
ヘッダーは設定していますか??
https://docs.github.com/en/rest/overview/resources-in-the-rest-api#user-agent-required
コード例
以下はissueを作成するコードです。
認証はパーソナルアクセストークンで行います。
PersonalAccessToken
, organization
, repository
はご自身のものに置き換えてください。
github.ts
importhttpsfrom'https'constcreateIssue=async(title:string,body:string,labels:string[]):Promise<number>=>{returnnewPromise((resolve,reject)=>{constreq=https.request({method:'POST',headers:{'User-Agent':'YourApp',// これ大事'Accept':'application/vnd.github.v3+json','Content-Type':'application/json','Authorization':`token ${PersonalAccessToken}`},host:'api.github.com',path:`/repos/${organization}/${repository}/issues`,},(response)=>{letres:any=''response.on('data',(chunk)=>{res+=chunk})response.on('end',()=>{res=JSON.parse(res)resolve(res.id)})response.on('error',(err:Error)=>reject(err))})req.write(buildPayload(title,body,labels))req.on('error',(err:Error)=>reject(err))req.end()})}constbuildPayload=(title:string,body:string,labels:string[]):string=>{returnJSON.stringify({title,body,labels})}
まとめ
全ての原因がこれに当てはまるかは分かりませんが、私はこれで解決できました。
(@octokit/rest.jsなどを使った方が早いかもしれませんね)