Github API responding with 'Content is not val

2019-05-31 04:05发布

I am getting a 'Content is not valid Base64' error when posting a certain, very simple, piece of content to github using the API. The content is

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

I am using Base64.urlsafe_encode64 to encode the content.

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
}

I am getting this reponse:

422
{"message":"content is not valid Base64",
 "documentation_url":"https://developer.github.com/v3/repos/contents/"}

I don't understand how this cannot be valid base64 for github. And it doesn't happen for all submitted data.

content='unit = $("<li  class=\'s   clearfix\'></li>")'
Base64.urlsafe_decode64(Base64.urlsafe_encode64(content))==content
=> true

What am I doing wrong?

2条回答
一纸荒年 Trace。
2楼-- · 2019-05-31 04:42

When I consider the Content API, I dno't see a field 'encoding' when you PUT a message.
I only see it in an answer (like in this one).

If you see the go-github project (in Go, but the idea is the same), you see the structure used for setting up a message: 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"`
}

The tests don't encode anything:

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

In your case (ruby), content should be enough.

查看更多
闹够了就滚
3楼-- · 2019-05-31 05:02

Turns out the content has to be encoded with Base64.strict_encode with complies with RFC 4648. I found the solution here on github: https://github.com/octokit/octokit.rb/blob/5323df945ecfd524556888e35d042a96c9055a1c/lib/octokit/client/contents.rb#L76

查看更多
登录 后发表回答