2

I am trying to use the GitHub api for creating a new file. As per the documentation

this can be done via a PUT request.

I am using the requests package for making the request. The GitHub api endpoint accepts valid json objects as input, i.e. where strings are within double quotes.

My data has the following format -

{
  'message': 'Updated learn.md',
  'content': 'ZW51bTM0PT0xLjEuNAotZSBnaXQraHR0cHM6Ly9naXRodWIuY29tL29wZW50b2svT3BlbnRvay1QeXRob24tU0RLLmdpdEAwMzU4YTI0ZDM0ZTkzMjVjYzRhODNhYmQxZTVjMGJmYzQ2M2ZkMjYwI2VnZz1vcGVudG9rCnB5dHo9PTIwMTYuNApyZXF1ZXN0cz09Mi4xMC4wCgo=',
  'branch': 'master'
}

Since I am putting the data which is in the form of a Python dictionary it is having single quotes, but GitHub isn't accepting it and is giving a 400 response of "Problems parsing JSON" as shown below

{
  "message": "Problems parsing JSON",
  "documentation_url": "https://developer.github.com/v3/repos/contents/#update-a-file"
}

I have verified that the problem is because of quotes only by using postman to make the PUT request, in that case it was successful.

The correct data is shown below

{
  "message": "Updated learn.md",
  "content": "ZW51bTM0PT0xLjEuNAotZSBnaXQraHR0cHM6Ly9naXRodWIuY29tL29wZW50b2svT3BlbnRvay1QeXRob24tU0RLLmdpdEAwMzU4YTI0ZDM0ZTkzMjVjYzRhODNhYmQxZTVjMGJmYzQ2M2ZkMjYwI2VnZz1vcGVudG9rCnB5dHo9PTIwMTYuNApyZXF1ZXN0cz09Mi4xMC4wCgo=",
  "branch": "master"
}

Here is how I am making the call

def put_data_to_github(self, url, data):
    headers = {}
    headers['Authorization'] = "token " + self.auth_token
    response = requests.put(url, data=data, headers=headers)
    return response

How can I make valid JSON objects in Python so that GitHub api accepts it? How Can I achieve this?

2
  • Please can you share the full Traceback error? Commented Apr 21, 2019 at 11:49
  • Included the error received in the Github response Commented Apr 21, 2019 at 11:52

1 Answer 1

7

You must convert your data to json before sending..

import json
...
response = requests.put(url, data=json.dumps(data), headers=headers)
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this solution earlier, but my data had a byte string, I decoded it and then used json.dumps, which did the trick. Thanks!
Hm...When using the Update File Contents Github API, the content key requires a b64 encoded value. I get a TypeError: Object of type bytes is not JSON serializable error when trying to use the above solution.
@SeaDude see the comment above yours for the solution.
Yes, I was reading that comment. When the base64 is decoded I can't use it as input for the content key. Here's the write-up.
Now I see what @Shahrukh-Mohammad was doing. I don't know why, but his suggestion coupled with changing the data parameter to json in the request.put() worked for me. See write up above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.