2014-04-09 76 views
1

当使用API​​向github发布特定的,非常简单的内容时,我得到'Content is not valid Base64'错误。内容是Github API响应'内容无效Base64'

unit = $("<li class='s clearfix'></li>"); 

我正在使用Base64.urlsafe_encode64来编码内容。

content = 'unit = $("<li class=\'s clearfix\'></li>")'; 
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 = $("<li class=\'s clearfix\'></li>")' 
Base64.urlsafe_decode64(Base64.urlsafe_encode64(content))==content 
=> true 

我在做什么错?

+1

你能给以'encode64'和'一试decode64'代替'urlsafe_decode64'和'urlsafe_encode64'? –

+0

原来我们必须使用Base64.strict_encode64(...)我在一个github lib中窥探,发现: https://github.com/octokit/octokit.rb/blob/5323df945ecfd524556888e35d042a96c9055a1c/lib/octokit /client/contents.rb#L76 –

+1

呵呵,不知道 - 感谢您跟进并告诉我! +优秀的侦探工作:) –

回答

1

当我考虑Content API时,当您放弃一条消息时,我看不到字段“编码”。
我只在答案中看到它(如in this one)。

如果你看到细末GitHub的项目(围棋,但这个想法是一样的),你会看到用于建立一个消息结构: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"` 
} 

tests don't encode anything

message := "m" 
content := []byte("c") 
repositoryContentsOptions := &RepositoryContentFileOptions{ 
    Message: &message, 
    Content: content, 
    Committer: &CommitAuthor{Name: String("n"), Email: String("e")}, 
} 

在你的情况(红宝石),content应该是足够的。

+0

Git实际上忽略了编码参数,所以你是对的。但我的问题是,我没有使用正确的base64编码,显然有差异。看到我的答案。 –