Kevin Clark
Kevin Clark

Reputation: 119

Use Github API with rest client to create a file

I'm trying to use the Github API to create a file in a repo.

This CURL command does exactly what I want to do:

curl -X PUT -H 'Authorization: token <TOKEN>' -d '{"path": "test.txt", "message": "Test Commit", "committer": {"name": "Kevin Clark", "email": "[email protected]"}, "content": "bXkgbmV3IGZpbGUgY29udGVudHM="}' https://api.github.com/repos/vernalkick/kevinclark/contents/test.txt

I need to do the same request but using rest_client in ruby, but this returns a 404:

require 'rest_client'

params = {
  :path => "test.txt",
  :message => "Test Commit",
  :committer => {
    :name => "Kevin Clark",
    :email => "[email protected]"
  },
  :content => "bXkgbmV3IGZpbGUgY29udGVudHM=",
  :access_token => <TOKEN>
}

RestClient.put "https://api.github.com/repos/vernalkick/kevinclark/contents/test.txt", :params => params

Github's documentation: https://developer.github.com/v3/repos/contents/

Upvotes: 1

Views: 259

Answers (1)

Kevin Clark
Kevin Clark

Reputation: 119

So I finally found the solution to my problem!

I needed to create a json string instead of just passing the hash.

RestClient.put "https://api.github.com/repos/vernalkick/kevinclark/contents/test.txt", :params => JSON.generate(params)

Upvotes: 4

Related Questions