Can I post a .json file on github using python requests?

Gevezo

So, here goes the line I'm trying to use:

 r=  requests.post('https://github.com/Gevez/gameficacao/upload/master', auth=HTTPBasicAuth('user', 'pass'), data='data.json')

The server returns a 403 code, even if I put the credentials correctly. Now I want to know if I'm doing it the wrong way or if github doesn't allow me to do it.

Bertrand Martel

You can use Github create repo content API for creating a file on your repository via the API :

PUT /repos/:owner/:repo/contents/:path

You would need to create a personal access token first. For example :

import requests 
import base64

token = "YOUR_TOKEN"

repo = 'bertrandmartel/test-repo'
path = 'data.json'

data = open("data.json", "r").read()

r = requests.put(
    f'https://api.github.com/repos/{repo}/contents/{path}',
    headers = {
        'Authorization': f'Token {token}'
    },
    json = {
        "message": "add new file",
        "content": base64.b64encode(data.encode()).decode(),
        "branch": "master"
    }
)
print(r.status_code)
print(r.json())

You can also use PyGithub library :

from github import Github

token = "YOUR_TOKEN"

repo = "bertrandmartel/test-repo"
path = "data.json"

# if using username and password
#g = Github("user", "password")

g = Github(token)

data = open("data.json", "r").read()

repo = g.get_repo(repo)
repo.create_file(
    path = path, 
    message = "add new file", 
    content = data, 
    branch = "master"
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python Requests: How can I properly submit a multipart/form POST using a file name

Post JSON using Python Requests

Post JSON using Python Requests

Can't post file using python requests - Translation from curl

Python Requests: Post JSON and file in single request

Using python requests to POST data with file

Can not find post file in github by using jekyll

Python requests post a file

Python Requests - post json

how to POST contents of JSON file to RESTFUL API with Python using requests module

Why can't I send `None` as data in a POST request using Python's `requests` library?

How can I catch the resulting page of a post request using requests?

Python Requests: POST JSON and file (multiform data) in 1 single request

Python requests library can't specify JSON key on HTTP POST

Post uploaded file using requests

How can I upload a not-utf-8 encoded file using Python's requests module?

How can I send a request to an HTML file on my PC as if it were a real server using Python/Requests?

unable to post file+data using python-requests

How I can I specify "page number" when webscraping dynamic HTML using Python's requests.post function?

How can i add some element into json file using python?

Why I can't get the same json results when opening browser and using requests python?

how to POST multipart list of JSON/xml files using python requests

flask python requests post using JSON data to localhost

I am not able to log in into a website using post requests python

How can I make json data from requests into excel file?

Can I reverse-proxy POST requests while using CloudFront CDN for GET requests?

How can I convert a CSV file to a JSON file with a nested json object using Python?

Why can't I download a midi file with python requests?

Python, can't webscrape the link to a pdf file using BS and requests