Explorex
Explorex

Reputation: 577

Python Get Request Printing specific key and value

I am new to HTTP requests, and I am trying to make a simple get request using Python and the Requests Library requesting GitHubs API.

I've currently tried to implement parameters for a key and value pair

import requests
r = requests.get("https://api.github.com/repos/git/git", params= {'name':name} )
print(name)

Obviously this is incorrect, as I'm getting an error which says name isn't defined which makes perfect sense however I don't know to print specific value's from keys I want rather the printing the entire r.json() response.

I've just tried to use this:

import requests
import json
r = requests.get("https://api.github.com/repos/git/git")
data = r.json()

class User:
    def __init__(self, json_def):
        self.__dict__ = json.loads(json_def)

user = User(data)
print(user.size)

However I'm getting the error:

TypeError: the JSON object must be str, bytes or bytearray, not 'dict'

Upvotes: 0

Views: 5212

Answers (1)

walugembe peter
walugembe peter

Reputation: 58

You are checking a Response object which contains a server’s response to an HTTP request.. From that link, I am guessing you are trying to check for contents from that response. So you can modify that code to this.

import requests
import json
r = requests.get("https://api.github.com/repos/git/git")
data = json.loads(r.content)

class User:
    def __init__(self, json_def):
        self.__dict__ = data

Upvotes: 1

Related Questions