0

I am following this tutorial : Create new repos using GITHUB API

I wrote that piece of code:

import requests
from pprint import pprint
import os

GIT_TOKEN = os.getenv('TMP_TOKEN')

payload = {
  'name': 'vbk_test',
  'private': 'true'
}

headers = {
  "Authorization": "Bearer " + GIT_TOKEN,
  "Accept": "application/vnd.github+json",
  "X-GitHub-Api-Version": "2022-11-28"
}

r = requests.post('https://api.github.com/user/repos', data=payload, headers=headers)
pprint(r.json())

and get this from pprint(r.json()):

{'documentation_url': 'https://docs.github.com/rest/repos/repos#create-a-repository-for-the-authenticated-user', 'message': 'Problems parsing JSON'}

of course no repo was created.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Vincent A
  • 85
  • 10
  • Does this answer your question? [How to POST JSON data with Python Requests?](https://stackoverflow.com/questions/9733638/how-to-post-json-data-with-python-requests) – mkrieger1 Jul 28 '23 at 19:10

1 Answers1

2

I was able to run your code and find the error, this happens because you are passing the data parameter in the POST request instead of json.

Make the following changes:

import requests
from pprint import pprint
import os

GIT_TOKEN = os.getenv('GITHUB_TOKEN')

payload = {
  'name': 'vbk_test',
  'private': 'true'
}

headers = {
  "Authorization": f"Bearer {GIT_TOKEN}",
  "Accept": "application/vnd.github+json",
  "X-GitHub-Api-Version": "2022-11-28"
}

r = requests.post(
    'https://api.github.com/user/repos', json=payload, headers=headers
)
pprint(r.json())

I continued the investigation because I wanted to know more about it and then I found this explanation on this point in more detail in the python requests library.

So from what I could understand, if you use the data parameter, you must pass a string in the exact format you want to send them to the server, and when using the data parameter you are responsible for performing the JSON-Encoded.

When using the json parameter, passing the dict object this will automatically be converted to JSON format, we can say the most comfortable way depending on your needs, the good thing about it is that currently Github API v3 supports JSON-Encoded POST/PATCH and that they deal with very well with either data or json requests.

See more about it here:

The example above I shared a solution using the json parameter (which automatically converts to JSON format), now I share the solution using the data parameter, the parameter of your question.

You must import JSON library and serialize your payload data string.

import os
import requests
from pprint import pprint
import json

GIT_TOKEN = os.getenv('GITHUB_TOKEN')

payload = {
  'name': 'vbk_test',
  'private': 'true'
}

headers = {
  "Authorization": f"Bearer {GIT_TOKEN}",
  "Accept": "application/vnd.github+json",
  "X-GitHub-Api-Version": "2022-11-28"
}

r = requests.post(
    'https://api.github.com/user/repos', data=json.dumps(payload), headers=headers
)
pprint(r.json())

Export the environment variable and then run:

export GITHUB_TOKEN=<your-github-token>

Hope this helps.

Akae Beka
  • 48
  • 3