Github API响应“内容无效Base64”
当使用API向github发布某个非常简单的内容时,我收到了“内容无效Base64”错误。 内容是
unit = $("");
我使用Base64.urlsafe_encode64来编码内容。
content = 'unit = $("")'; url = "https://api.github.com/repos/#{github_user}/#{github_repo}/contents/#{path}" RestClient.put(url, { message: "my message", content: Base64.urlsafe_encode64(content), encoding:"base64" }.to_json, { params:{access_token:access_token },accept:'json'}){ |response, request, result| puts response.code puts response }
我得到了这个回应:
422 {"message":"content is not valid Base64", "documentation_url":"https://developer.github.com/v3/repos/contents/"}
我不明白这对github来说是不是有效的base64。 并且所有提交的数据都不会发生。
content='unit = $("")' Base64.urlsafe_decode64(Base64.urlsafe_encode64(content))==content => true
我究竟做错了什么?
事实certificate,内容必须使用符合RFC 4648的Base64.strict_encode进行编码。我在github上找到了解决方案: https : //github.com/octokit/octokit.rb/blob/5323df945ecfd524556888e35d042a96c9055a1c/lib/octokit/client/ contents.rb#L76
当我考虑Content API时 ,在您输入消息时我看不到字段’encoding’。
我只在一个答案中看到它(就像在这个答案中 )。
如果你看到go-github项目(在Go中,但想法是相同的),你会看到用于设置消息的结构: RepositoryContentFileOptions
:
// RepositoryContentFileOptions specifies optional parameters for CreateFile, UpdateFile, and DeleteFile. type RepositoryContentFileOptions struct { Message *string `json:"message,omitempty"` Content []byte `json:"content,omitempty"` SHA *string `json:"sha,omitempty"` Branch *string `json:"branch,omitempty"` Author *CommitAuthor `json:"author,omitempty"` Committer *CommitAuthor `json:"committer,omitempty"` }
测试不编码任何东西 :
message := "m" content := []byte("c") repositoryContentsOptions := &RepositoryContentFileOptions{ Message: &message, Content: content, Committer: &CommitAuthor{Name: String("n"), Email: String("e")}, }
在你的情况下(ruby), content
应该足够了。