#🔒 Github API

59 messages · Page 1 of 1 (latest)

sleek viper
#

I used this https://github.com/orgs/community/discussions/24723 as reference on how to update repositires. While trying to do that I encountered some errors.
The response of the code below is

400
{"message":"Problems parsing JSON","documentation_url":"https://docs.github.com/rest/repos/contents#create-or-update-file-contents","status":"400"}

Code:

    headers = {
        'accept': 'application/vnd.github+json',
        'Authorization': f"Bearer {TOKEN}",
        'merge_commit_message' :'Formulated questions',
        'merge_commit_title': 'Updating Worksheets'
    }
    
    # data = {
    #     'name': 'Worksheets', 
    #     'description': 'Using Github API', 
    #     'private':'false',
        
    # }
    
    # response = httpx.get(url, params=params_)
    # print(response.text)
    response = httpx.put(url, headers=headers, content=example_data)

Now I do not know why it can't parse the json despite my(or at least I think so) data dictionary arising directly from github docs.
https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#update-a-repository

If you have any idea on how to fix this problem, it would be great. Thanks

mellow moonBOT
#

@sleek viper

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

vernal elbow
#

What is example_data

#

Also, use json= instead

#

Otherwise if you use content= with a dictionary, it might stringify it not in a way that comply with JSON

sleek viper
#

its a string

sleek viper
vernal elbow
#

Hmm

#

Let me recheck

#

Can you check how should the API be used?

sleek viper
#

I did. I posted a discussion thread above in my initial reply mate.

vernal elbow
#

What is your url?

shrewd cosmos
#

the link to "update a repository" shows a "patch" request. Your code seems to be using "put".
The payload seems to be indeed json. so you probably need to use json= in your request, instead of content=, to ensure the dictionary is converted to json.

vernal elbow
#

You reference to the wrong API endpoint and mixing and matching stuff

#

Or you just give us completely wrong API docs

sleek viper
shrewd cosmos
#

what is example_data? if it's a dictionary, then you should use json=

sleek viper
#

its a string

shrewd cosmos
#

how did you make that string?

sleek viper
#

wdym, how? I didn't make it anythinig. I'm trying to create a file whose basename is .md and contents to be uploaded to the file is example_data

shrewd cosmos
#

as far as I can tell the payload needs to be a json structure.

#

the files content is part of that "content" key, as base64 encoded. that's what it looks like to me

sleek viper
#

for some reason I seem to be using a different docs

#

i'll look into the one sent above and try

#

leme see if i can fix it

shrewd cosmos
#

you linked them in your original post 🙂

#

(not those exactly)

#

actually, they're linked in the error message

sleek viper
sleek viper
shrewd cosmos
vernal elbow
sleek viper
# shrewd cosmos

Well I did change some stuff


    headers = {
        'accept': 'application/vnd.github+json',
        'Authorization': f"Bearer {TOKEN}",
    }
    
    content_info = {
        'message': 'Formulated Questions', 
        'content': example_data
    }
    
    response = httpx.put(url, headers=headers, data=content_info)

But the message still persists.

400
{"message":"Problems parsing JSON","documentation_url":"https://docs.github.com/rest/repos/contents#create-or-update-file-contents","status":"400"}

url = f"{BASE_URL}/repos/Inheritanc-e/Worksheets/contents/Unit1-2.md"

shrewd cosmos
#

data= will not encode the payload as json

#

you need to use json= so that the payload is json encoded.

sleek viper
#

Right, i forgot about that. After I did do that

    content_info = {
        'message': 'Formulated Questions', 
        'content': example_data.encode('utf-8')
    }
    
    response = httpx.put(url, headers=headers, json=content_info)

Before encoding example_data the error I got was
After encoding it I got,

BEFORE:

{"message":"content is not valid Base64","documentation_url":"https://docs.github.com/rest/repos/contents#create-or-update-file-contents","status":"422"}


AFTER: 

TypeError: Object of type bytes is not JSON serializable
shrewd cosmos
#

content is not valid Base64

vernal elbow
#

Python would stringify dictionary quote as single quote while json need double quote

sleek viper
#

ooo

#

i mistook base64 and bytes as the same thing

#

my bad fellas

shrewd cosmos
#

base64.b64encode

vernal elbow
#

Base64 is using 64 character ([a-zA-Z0-9+/]) to represent binary data and = as placeholder for at the end

#

To prevent exist of encoded character is a special character that would break formatting

sleek viper
#

Breh, i'm so sorry man idk character types, that's why some questions might be repetitive.

Traceback (most recent call last):
  File "/Users/inheritanc-e/Documents/Python_Work/Projects/language_helper/__main__.py", line 32, in <module>
    example_data = base64.b64encode("**Genitive and Dative Cases Worksheet**")
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/base64.py", line 58, in b64encode
    encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'
shrewd cosmos
#

it accepts bytes and returns bytes. so you need to encode it (or use binary mode for files) and then decode the result afterwards

#

base64.b64encode(example_data.encode('utf-8')).decode()

#

or base64.b64encode(b"raw text").decode() if you want to test first

sleek viper
#

Hmm I see. I did get 201, leme try playing around with the text before I close the thread. Apprecieate the help @shrewd cosmos

#

and @vernal elbow

vernal elbow
#

Ok

sleek viper
#

Alright, it seems to work

#

thankyou fellas

#

!close

mellow moonBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.