Search code examples
apipostmangithub-apigistgithub-api-v3

Automating GitHub API for creating repos/Gists return 200 OK instead of 201 Created


I use Postman to Create a gist and I added bearer token in the authorization Tab but it suppose to create a gist and return 201 Created instead it returns 200 OK and it doesn't create anything enter image description here

I have written in the request body the example mentioned at GitHub Docs to create a Gist

{
  "description": "Hello World Examples",
  "public": true,
  "files": {
    "hello_world.rb": {
      "content": "class HelloWorld\n   def initialize(name)\n      @name = name.capitalize\n   end\n   def sayHi\n      puts \"Hello !\"\n   end\nend\n\nhello = HelloWorld.new(\"World\")\nhello.sayHi"
    },
    "hello_world.py": {
      "content": "class HelloWorld:\n\n    def __init__(self, name):\n        self.name = name.capitalize()\n       \n    def sayHi(self):\n        print \"Hello \" + self.name + \"!\"\n\nhello = HelloWorld(\"world\")\nhello.sayHi()"
    },
    "hello_world_ruby.txt": {
      "content": "Run `ruby hello_world.rb` to print Hello World"
    },
    "hello_world_python.txt": {
      "content": "Run `python hello_world.py` to print Hello World"
    }
  }
}

Solution

  • https://docs.github.com/en/free-pro-team@latest/rest/reference/gists#create-a-gist

    Authentication You can read public gists anonymously, but you must be signed into GitHub to create gists. To read or write gists on a user's behalf, you need the gist OAuth scope and a token. For more information, see "Scopes for OAuth Apps."

    You should be authenticated else you will have only read access, thats why you are getting 200 instead of 201

    Second Reason: you are using http instead of https

    Use token generated from developer settings as oauth2 bearer:

    enter image description here