How do I get plain text from an API?

Viewed 65

I'm sorry just asking like this, but I'm new to JSON and API-related Stuff.

@bot.command()
async def insult(ctx):
  resp = requests.get("https://insult.mattbas.org/api/insult")
  if 300 > resp.status_code >= 200:
      content = resp.json()  #We have a dict now.
  else:
      content = f"Recieved a bad status code of {resp.status_code}."
  await ctx.send(content)

The API displays plain text and I can't figure out how to get it. I can however get other API results which do not have plain text.

Please help.

1 Answers

You should call resp.content or resp.text instead of json().

@bot.command()
async def insult(ctx):
  resp = requests.get("https://insult.mattbas.org/api/insult")
  if 300 > resp.status_code >= 200:
      content = resp.content  #We have a dict now.
  else:
      content = f"Recieved a bad status code of {resp.status_code}."
  await ctx.send(content)
Related