How to get data from a specific API website using Python

Question:

So I’m trying to randomly generate an insult from this API. I’m getting a response 200 from the API but I can’t seem to extract data

I’m using this code:

def get_insult():
  res = requests.get('https://insult.mattbas.org/api/insult.txt')
  print(res)
  data_json = json.loads(res.json())
  print(data_json)
  

get_insult()

Answers:

This should work. You don’t need to use json.loads() as the response is not json.

 def get_insult():
      res = requests.get('https://insult.mattbas.org/api/insult.txt')
      data_json = res.text
      print(data_json)
      
 get_insult()
Answered By: Ikram Khan Niazi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.