2014-04-09 75 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編碼,顯然有差異。看到我的答案。 –